rubenhak
rubenhak

Reputation: 890

Node.js Promise without returning it

I have a use case to resolve a Promise without returning it. Catching for errors internally, but don't want the caller to wait for the promise to resolve.

doSomething()
{
    Promise.resolve()
        .then(() => {
            // do something.
        })
        .catch(reason => {
            this.logger.error(reason);
        });
}

Getting this error:

(node:2072) Warning: a promise was created in a handler at internal/timers.js:439:21 but was not returned from it, see http://. goo.gl/rRqMUw
    at Function.Promise.cast (.../node_modules/bluebird/js/release/promise.js:225:13)

Upvotes: 0

Views: 349

Answers (1)

Radu Diță
Radu Diță

Reputation: 14181

Just return something from the Promise callback where you are creating the fire and forget promise.

I'm guessing that handler is doSomething

doSomething()
{
    Promise.resolve()
    .then(() => {
        // do something.
    })
    .catch(reason => {
        this.logger.error(reason);
    });

    return null //or anything else that's sensible
}

Note: We usually ignore the error message, but sometimes they contain valuable information. In your error there's a link http://. goo.gl/rRqMUw that explains exactly this problem:d

Upvotes: 1

Related Questions