npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

tot

v0.2.0

Published

Experimental language, superset of JavaScript, for writing async code like sync code

Downloads

7

Readme

tot

Experimental language, superset of JavaScript, for writing async code like sync code.

Example

With tot you can call async functions like if they were sync. For exmaple you have the following funciton:

function something(message, callback) {
  setTimeout(function() {
    callback(null, 'hello '+message)
  }, 1)
}

Tot adds a new operator =: that lets you call this function like this:

var world =: something('world')

You can even declare multiple returning variables:

var foo, bar =: someFunction('world')

Full example

Full example (example.tot)

function something(message, callback) {
  setTimeout(function() {
    // if (message === 'bar') return callback(new Error('bar is not supported'))
    callback(null, 'hello '+message)
  }, 1)
}

function done() {
  var args = Array.prototype.slice.call(arguments)
  console.log('Done')
  console.log(args.join('\n'))
}

exports.hello = function() {
  try {
    var foo =: something('foo')
    var bar =: something('bar')
    var baz =: something('baz')

    done(foo, bar, baz)
  } catch(e) {
    console.log('error', e.message)
  }

}

Usage

First init the npm module (install it with npm install tot --save)

require('tot')

Then you can require any .tot file.

require('./example').hello()

## Benefits

  • Any async function can be used
  • You don't have to change the way you create async functions
  • It is simple as hell

How it works

Tot compiles to JavaScript. It converts this:

var foo =: something('foo')
var bar =: something('bar')

into this

something('foo', function(err, foo) {
  something('bar', function(err, foo) {
    
  })
})

And if you surround the code within a try - catch it also handles the error in every callback using the code you wrote in the catch block.

Things to have in mind

Tot is block-based

When using =: all the code from that operator to the end of the current block will be executed asynchronously (even though the syntax looks sync code). But anything outside the current block will be executed synchronously. For example:

if (condition) {
  var foo =: something('foo')
  console.log('world')
}
console.log('hello')

Tot generates the following code for this example:

if (condition) {
  something('foo', function(err, foo) {
    console.log('world')
  })
}
console.log('hello')

So console.log('hello') is always executed before console.log('world')

Arguments

The special arguments variable available in JavaScript when executing a function is also available but after any use of =: it will not match what you expect. If you want to use it, make a copy of it first:

// make a copy into an array
var args = Array.prototype.slice.call(arguments)
var foo =: something('foo')
// do not use `arguments` here

How it doesn't work

  • Tot does not create threads
  • Tot does not spawn processes

Sysntax highlighting in Sublime Text

Open a .tot file and go to the menu View → Syntax → Open all with current extension as... → JavaScript → JavaScript and all .tot files will be highlighted as JavaScript. Even though the =: does not exist in JavaScript the syntax highlighting will work pretty well.

Ignore intermediate js files

You can add *.tot.js in your .gitignore file to ignore intermediate js files generated by tot.

Drawbacks

Since Tot compiles to JavaScript any stack trace will reference lines of code of the generated JavaScript and not the original Tot code. This could be fixed using sourcemaps in a future.