Skip to content

Instantly share code, notes, and snippets.

@Jxck
Created October 17, 2012 16:01
Show Gist options
  • Save Jxck/3906371 to your computer and use it in GitHub Desktop.
Save Jxck/3906371 to your computer and use it in GitHub Desktop.
非同期と next()
log = console.log.bind(console);
var assert = require('assert');
function test() {
var tests = Array.prototype.slice.call(arguments);
function next() {
if(!tests.length) return;
var args = Array.prototype.slice.call(arguments);
args.push(next);
var t = tests.shift();
t.apply(null, args);
};
next();
};
test(function(next) {
var a = 0;
a ++; log(a);
assert.equal(a, 1);
next(a, 1);
}, function(a, b, next) {
setTimeout(function() {
a += b; log(a);
assert.equal(a, 2);
next(a);
}, 200);
}, function(a, next) {
setTimeout(function() {
a ++; log(a);
assert.equal(a, 3);
next();
}, 100);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment