Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed #1314: yielding a function should not call the function #1315

Merged
merged 2 commits into from
Jan 27, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/generators.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ PromiseSpawn.prototype._continue = function (result) {
if (maybePromise === null) {
this._promiseRejected(
new TypeError(
YIELDED_NON_PROMISE_ERROR.replace("%s", value) +
YIELDED_NON_PROMISE_ERROR.replace("%s", String(value)) +
FROM_COROUTINE_CREATED_AT +
this._stack.split("\n").slice(1, -7).join("\n")
)
Expand Down
40 changes: 28 additions & 12 deletions test/mocha/generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,22 @@ describe("yielding", function() {
assert.equal(val, 4);
});
});

specify("yielding a function should not call the function", function() {
let functionWasCalled = false;
return Promise.coroutine(function*(){
try {
yield (function() {functionWasCalled = true;});
}
catch(e){
assert(e instanceof TypeError);
assert.equal(functionWasCalled, false);
return 4;
}
})().then(function(val){
assert.equal(val, 4);
});
});
});

describe("thenables", function(){
Expand Down Expand Up @@ -367,7 +383,7 @@ describe("Spawn", function() {
describe("custom yield handlers", function() {
specify("should work with timers", function() {
var n = 0;
return Promise.coroutine.addYieldHandler(function(v) {
Promise.coroutine.addYieldHandler(function(v) {
if (typeof v === "number") {
n = 1;
return Promise.resolve(n);
Expand Down Expand Up @@ -417,18 +433,18 @@ describe("custom yield handlers", function() {
});
});

Promise.coroutine.addYieldHandler(function(v) {
if (typeof v === "function") {
var cb;
var promise = Promise.fromNode(function(callback) {
cb = callback;
});
try { v(cb); } catch (e) { cb(e); }
return promise;
}
});

specify("should work with thunks", function(){
Promise.coroutine.addYieldHandler(function(v) {
if (typeof v === "function") {
var cb;
var promise = Promise.fromNode(function(callback) {
cb = callback;
});
try { v(cb); } catch (e) { cb(e); }
return promise;
}
});

var thunk = function(a) {
return function(callback) {
setTimeout(function(){
Expand Down