Skip to content

Commit

Permalink
Merge pull request #1175 from not-an-aardvark/async-yield-handling
Browse files Browse the repository at this point in the history
Ensure Promise.coroutine always handles yielded Promises asynchronously
  • Loading branch information
spion committed Aug 3, 2016
2 parents 1f52e44 + 5e597e0 commit 5291d20
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/generators.js
Expand Up @@ -175,9 +175,13 @@ PromiseSpawn.prototype._continue = function (result) {
this._yieldedPromise = maybePromise;
maybePromise._proxy(this, null);
} else if (BIT_FIELD_CHECK(IS_FULFILLED)) {
this._promiseFulfilled(maybePromise._value());
Promise._async.invoke(
this._promiseFulfilled, this, maybePromise._value()
);
} else if (BIT_FIELD_CHECK(IS_REJECTED)) {
this._promiseRejected(maybePromise._reason());
Promise._async.invoke(
this._promiseRejected, this, maybePromise._reason()
);
} else {
this._promiseCancelled();
}
Expand Down
34 changes: 34 additions & 0 deletions test/mocha/generator.js
Expand Up @@ -243,6 +243,40 @@ describe("Promise.coroutine", function() {
assert.equal(e, error);
});
});

specify("when they are already fulfilled, the yielded value should be returned asynchronously", function(){
var value;

var promise = Promise.coroutine(function*(){
yield Promise.resolve();
value = 2;
})();

value = 1;

return promise.then(function(){
assert.equal(value, 2);
});
});

specify("when they are already rejected, the yielded reason should be thrown asynchronously", function(){
var value;

var promise = Promise.coroutine(function*(){
try {
yield Promise.reject();
}
catch (e) {
value = 2;
}
})();

value = 1;

return promise.then(function(){
assert.equal(value, 2);
});
});
});

describe("yield loop", function(){
Expand Down

0 comments on commit 5291d20

Please sign in to comment.