node/test/simple/test-next-tick-ordering.js

33 lines
598 B
JavaScript
Raw Normal View History

2010-12-05 07:20:34 +08:00
var common = require('../common');
var assert = require('assert');
var i;
var N = 333;
var done = [];
function get_printer(timeout) {
2010-12-03 09:03:18 +08:00
return function() {
console.log('Running from setTimeout ' + timeout);
done.push(timeout);
};
}
2010-12-03 09:03:18 +08:00
process.nextTick(function() {
console.log('Running from nextTick');
done.push('nextTick');
2010-12-03 09:03:18 +08:00
});
for (i = 0; i < N; i += 1) {
setTimeout(get_printer(i), i);
}
2010-12-03 09:03:18 +08:00
console.log('Running from main.');
2010-12-03 09:03:18 +08:00
process.addListener('exit', function() {
assert.equal('nextTick', done[0]);
for (i = 0; i < N; i += 1) {
2010-12-03 09:03:18 +08:00
assert.equal(i, done[i + 1]);
}
});