Created
October 17, 2012 16:01
-
-
Save Jxck/3906371 to your computer and use it in GitHub Desktop.
非同期と next()
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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