Skip to content

Promises returned from generators are not resolved automatically #64

Closed

Description

@dstillman

In Q, if an async generator returns a promise, that promise is resolved and the resolved value is passed on. In Bluebird, the promise itself is passed along as the next value:

Q v1.0.0:

Q.async(function* () {
    return Promise.resolve("")
})()
.then(function (str) {
    alert(typeof str); // 'string'
})
.done();

Bluebird v0.11.6-1:

Promise.spawn(function* () {
    return Promise.resolve("");
})
.then(function (str) {
    alert(typeof str); // 'object'
});

The workaround in Bluebird is to yield on the promise before returning it:

Promise.spawn(function* () {
    return yield Promise.resolve("");
})
.then(function (str) {
    alert(typeof str); // 'string'
});

But that seems like an unnecessary extra step that could be avoided, and I think it goes against expectations coming from Promise.try(). Is this behavior intentional?

Activity

  1. benjamingr commented on Jan 11, 2014

    @benjamingr
    Collaborator

    To be fair, in C# you would write return await and not return to return from an async function.

    The Bluebird behavior seems nice (and correct) but I see your point - this is inconsistent with the way promises unwrap. Definitely something we need to think about.

  2. petkaantonov commented on Jan 11, 2014

    @petkaantonov
    Owner

    Yea confirmed as bug.

  3. benjamingr commented on Jan 11, 2014

    @benjamingr
    Collaborator

    @petkaantonov are you sure returning a promise acting like in thenables is the correct behavior?

  4. petkaantonov commented on Jan 11, 2014

    @petkaantonov
    Owner

    Yes it is always without any doubt a bug if you get a promise as a .then argument

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions