ysmood / yaku

A lightweight promise library
https://tonicdev.com/ysmood/yaku
MIT License
291 stars 28 forks source link

difference between return Promise.reject(x) and throw x #48

Closed lukeapage closed 7 years ago

lukeapage commented 7 years ago

I can track down further if its not obvious but I've been trying to work out why I get unhandled rejections for promises that are handled. I've not been able to make a reduced test case, but the behaviour seems to occur when I have something like

promise.catch(() => {
    // maybe do something different
    return Promise.reject({});
}).catch(() => {
    // absorb rejection so unhandledrejection should not be called
});

and it appears changing the return to throw make it work and not give unhandled rejections (which is the expected behaviour).

lukeapage commented 7 years ago

Sorry.. all attempts I've made at a cutdown reproducible have failed, but maybe you can think of something that might help?

ysmood commented 7 years ago

Thanks for your report, I'll look into it.

ysmood commented 7 years ago

Have you read this, maybe it helps : https://github.com/ysmood/yaku/blob/master/docs/debugHelperComparison.md

lukeapage commented 7 years ago

Yes, but the problem is an unhandled rejection (or 2 actually for the same promise chain) that I do not expect to see. If switching to throw hadn't solved it I would have investigated more.

ysmood commented 7 years ago
var Promise = Yaku = require("yaku");

var promise = Yaku.reject()

promise.catch(() => {
    return Promise.reject();
}).catch(() => {
});

For me the code above works without any problem. So the question is still how to reproduce your issue.

lukeapage commented 7 years ago

Yep and I've tried more complicated cases. I think I'll have to experiment/investigate.

I'm wondering if one of the promises on my chain is using the native promise and thats why. I'll add alot of debugging and get back to you.

ysmood commented 7 years ago

In most cases, you just forgot to write a return.

Or you can add a line before everything to wipe out native Promise noise:

var Promise = require("yaku");
// browser
window.Promise = Promise

// node.js
global.Promise = Promise