Skip to content Skip to sidebar Skip to footer

Receiving `unhandledpromiserejectionwarning` Even Though Rejected Promise Is Handled

I have constructed a function which iterates through a Generator containing both synchronous code and Promises: module.exports = { isPromise (value) { return type

Solution 1:

What am I overlooking?

Your yieldedOutPromise.value.then call is creating a new promise, and if yieldedOutPromise.value rejects then it will be rejected as well. It doesn't matter that you handle the error via .catch on yieldedOutPromise.value, there's still a rejected promise around, and it's the one that will be reported.

You are basically splitting your promise chain, which leads to each end needing an error handler. However, you shouldn't be splitting anything at all. Instead of the

promise.then(onSuccess);
promise.catch(onError);

antipattern you should use

promise.then(onSuccess, onError).…

Oh, and while you're at it, avoid the Promise constructor antipattern. Just do

module.exports = functionrunGen (generatorFunc, startValue) {
    returnPromise.resolve(startValue).then(generatorFunc).then(generator => {
        returnkeepIterating({done: false, value: undefined});
        functionkeepIterating({done, value}) {
            if (done) return value;
            returnPromise.resolve(value).then(fulfilledValue =>
                generator.next(fulfilledValue)
            , err =>
                generator.throw(err)
            ).then(keepIterating);
        }
    });
};

Post a Comment for "Receiving `unhandledpromiserejectionwarning` Even Though Rejected Promise Is Handled"