Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/gimenete/tot
Experimental language, superset of JavaScript, for writing async code like sync code
https://github.com/gimenete/tot
Last synced: 27 days ago
JSON representation
Experimental language, superset of JavaScript, for writing async code like sync code
- Host: GitHub
- URL: https://github.com/gimenete/tot
- Owner: gimenete
- License: mit
- Created: 2015-05-19T22:04:05.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2015-05-20T17:34:08.000Z (over 9 years ago)
- Last Synced: 2024-04-14T23:06:44.506Z (9 months ago)
- Language: JavaScript
- Size: 145 KB
- Stars: 4
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
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:
```javascript
function something(message, callback) {
setTimeout(function() {
callback(null, 'hello '+message)
}, 1)
}
```Tot adds a new operator `=:` that lets you call this function like this:
```javascript
var world =: something('world')
```You can even declare multiple returning variables:
```javascript
var foo, bar =: someFunction('world')
```## Full example
Full example (example.tot)
```javascript
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`)
```javascript
require('tot')
```Then you can require any `.tot` file.
```javascript
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:
```javascript
var foo =: something('foo')
var bar =: something('bar')
```into this
```javascript
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:
```javascript
if (condition) {
var foo =: something('foo')
console.log('world')
}
console.log('hello')
```Tot generates the following code for this example:
```javascript
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:
```javascript
// 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.