Skip to content

Unhandled promise rejection when an inner promise rejects #46

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
Moneil97 opened this issue Dec 18, 2023 · 0 comments
Open

Unhandled promise rejection when an inner promise rejects #46

Moneil97 opened this issue Dec 18, 2023 · 0 comments

Comments

@Moneil97
Copy link

Using Promise 4.0.1, when I run the following code:

Promise(function(resolve, reject){
    throw "hello"
})
.fail(function(err){
    server.error("fail: " + err);
})

The fail handler catches the error and I get this output, which is what I would expect: ERROR: fail: hello

However, if I change the code so that it has an inner promise that throws:

Promise(function(resolve, reject){
    return Promise(function(resolve2, reject2){
        throw "hello"
    })
})
.fail(function(err){
    server.error("fail: " + err);
})

I would still expect the same output, but now I get Unhandled promise rejection: hello

I get the same behavior if I use a Promise.reject instead of throw:

Promise(function(resolve, reject){
    return Promise.reject("hello")
})
.fail(function(err){
    server.error("fail: " + err);
})

If I now add another fail handler:

Promise(function(resolve, reject){
    return Promise(function(resolve2, reject2){
        throw "hello"
    })
    .fail(function(err){
        server.error("inner fail: " + err);
        throw err;
    })
})
.fail(function(err){
    server.error("fail: " + err);
})

I get:

ERROR: inner fail: hello
Unhandled promise rejection: hello

The workaround I have found is to setup my promises like this:

Promise.resolve(null).then(function(_){
    return Promise(function(resolve2, reject2){
        throw "hello"
    })
    .fail(function(err){
        server.error("inner fail: " + err);
        throw err;
    })
})
.fail(function(err){
    server.error("fail: " + err);
})

now I get the output I would expect:

ERROR: inner fail: hello
ERROR: fail: hello
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant