Search code examples
node.jspromisebluebird

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)

Solution

  • 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