Skip to content

Commit 4599e7d

Browse files
committed
prefer setImmediate over setTimeout, and don't use process.nextTick in
Promise
1 parent abb166e commit 4599e7d

File tree

2 files changed

+7
-9
lines changed

2 files changed

+7
-9
lines changed

lib/promise.js

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -412,14 +412,12 @@
412412
func.call();
413413
};
414414
if (AV.Promise._isPromisesAPlusCompliant) {
415-
if (typeof(window) !== 'undefined' && window.setTimeout) {
415+
if (typeof(setImmediate) !== 'undefined' && _.isFunction(setImmediate)) {
416+
runLater = setImmediate;
417+
} else if (typeof(setTimeout) !== 'undefined' && _.isFunction(setTimeout)) {
416418
runLater = function(func) {
417-
window.setTimeout(func, 0);
418-
};
419-
} else if (typeof(process) !== 'undefined' && process.nextTick) {
420-
runLater = function(func) {
421-
process.nextTick(func);
422-
};
419+
window.setImmediate(func);
420+
}
423421
}
424422
}
425423

test/promise.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ describe('promise', function() {
162162
expect(errors[2]).to.be(64);
163163
expect(errors[3]).to.be(128);
164164
//should be 128 ms
165-
expect(Date.now() - startDate).to.be.within(125,140);
165+
expect(Date.now() - startDate).to.be.within(125, 145);
166166

167167
done();
168168
}).done(function(ret){
@@ -181,7 +181,7 @@ describe('promise', function() {
181181
]).catch(function (error) {
182182
expect(error).to.be(1);
183183
//should be 1 ms
184-
expect(Date.now() - startDate).to.be.within(0,5);
184+
expect(Date.now() - startDate).to.be.within(0, 10);
185185
setTimeout(done, 500);
186186
}).done(function(ret){
187187
throw ret;

0 commit comments

Comments
 (0)