tamejs
JavaScript code rewriter for taming async-callback-style code
I want to use tamejs to write mocha tests, but mocha doesn't support it by default.
Is there any way to let mocha work with tamejs? I don't want to write .tjs
files and compiling them into .js
each time before running tests.
Source: (StackOverflow)
I'm interested in tamejs, and want to use it in my project.
But I don't want to manually compile them into js files before running/testing, etc. I want to let tamejs monitor a directory, and automatically compiles .tjs to .js when the files change.
Is there any way to do this?
Source: (StackOverflow)
See my test code using mocha + tamejs:
test/t.tjs
require('should');
function inc(n, callback) {
setTimeout(function() {
console.log('### inc: ' + n);
callback(n+1);
}, 1000);
};
describe('test', function(){
it('show ok with tamejs', function(){
console.log('### testing ...');
var result;
await { inc(1, defer(result)); }
console.log('result: ' + result);
result.should.equal(123456); // won't pass
});
});
Compile it to t.js:
tamejs -o test/t.js test/t.tjs
Run mocha
mocha
Result:
### testing ...
.
✔ 1 test complete (1ms)
It seems the inc
method has never been invoked.
Source: (StackOverflow)
I'm pretty happy with tamejs, it makes my javascript code much clearer. But I still feel the error handling is a little boring.
See the code:
// callback should be callback(err, nextInt)
function inc(n, callback) {
setTimeout(function() {
callback(null, n+1);
}, 100);
}
await { inc(3, defer(var err, next));}
if(err) throw new Error(err); // !!! error handling
await { inc(8, defer(var err, next));}
if(err) throw new Error(err); // !!! error handling
await { inc(12, defer(var err, next));}
if(err) throw new Error(err); // !!! error handling
await { inc(39, defer(var err, next));}
if(err) throw new Error(err); // !!! error handling
Since nearly every asynchronous api has callbacks which have a error
as the first parameter, we need to get it and check it first.
You can see there are a lot of error handling lines in the sample, which is boring.
Is there any way to simplify it?
Source: (StackOverflow)