diff --git a/src/async.js b/src/async.js index bf08ffb4a..2f5a49569 100644 --- a/src/async.js +++ b/src/async.js @@ -7,6 +7,7 @@ var Queue = require("./queue"); var util = require("./util"); function Async() { + this._customScheduler = false; this._isTickUsed = false; this._lateQueue = new Queue(LATE_QUEUE_CAPACITY); this._normalQueue = new Queue(NORMAL_QUEUE_CAPACITY); @@ -19,6 +20,17 @@ function Async() { this._schedule = schedule; } +Async.prototype.setScheduler = function(fn) { + var prev = this._schedule; + this._schedule = fn; + this._customScheduler = true; + return prev; +}; + +Async.prototype.hasCustomScheduler = function() { + return this._customScheduler; +}; + Async.prototype.enableTrampoline = function() { this._trampolineEnabled = true; }; diff --git a/src/promise.js b/src/promise.js index bad586a52..42f38d0e1 100644 --- a/src/promise.js +++ b/src/promise.js @@ -210,9 +210,7 @@ Promise.setScheduler = function(fn) { if (typeof fn !== "function") { throw new TypeError(FUNCTION_ERROR + util.classString(fn)); } - var prev = async._schedule; - async._schedule = fn; - return prev; + return async.setScheduler(fn); }; Promise.prototype._then = function ( @@ -324,6 +322,7 @@ Promise.prototype._setCancelled = function() { }; Promise.prototype._setAsyncGuaranteed = function() { + if (async.hasCustomScheduler()) return; this._bitField = this._bitField | IS_ASYNC_GUARANTEED; }; diff --git a/test/mocha/promisify.js b/test/mocha/promisify.js index 6e99929b6..87b1b7664 100644 --- a/test/mocha/promisify.js +++ b/test/mocha/promisify.js @@ -1092,3 +1092,25 @@ describe("github 1063", function() { }); }) }); + +describe("github 1023", function() { + specify("promisify triggers custom schedulers", function() { + var triggered = false; + var defaultScheduler = Promise.setScheduler(function(fn) { + triggered = true; + setTimeout(fn, 0); + }); + var fnAsync = Promise.promisify(function(cb) { + setTimeout(function() { + cb(null, true); + }, 0); + }); + + return fnAsync().then(function(result) { + assert(result); + assert(triggered); + }).lastly(function() { + Promise.setScheduler(defaultScheduler); + }); + }); +})