Skip to content

Commit

Permalink
Fixes #1023
Browse files Browse the repository at this point in the history
  • Loading branch information
petkaantonov committed Apr 12, 2016
1 parent 7d00cde commit f2a58f8
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 3 deletions.
12 changes: 12 additions & 0 deletions src/async.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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;
};
Expand Down
5 changes: 2 additions & 3 deletions src/promise.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -324,6 +322,7 @@ Promise.prototype._setCancelled = function() {
};

Promise.prototype._setAsyncGuaranteed = function() {
if (async.hasCustomScheduler()) return;
this._bitField = this._bitField | IS_ASYNC_GUARANTEED;
};

Expand Down
22 changes: 22 additions & 0 deletions test/mocha/promisify.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});
})

0 comments on commit f2a58f8

Please sign in to comment.