Skip to content
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

putting catch before then in the chain, intended behavior? #1251

Closed
wayofthefuture opened this issue Oct 1, 2016 · 2 comments
Closed

putting catch before then in the chain, intended behavior? #1251

wayofthefuture opened this issue Oct 1, 2016 · 2 comments

Comments

@wayofthefuture
Copy link

So to make our code more concise, we implement a module-wide error function catch. It really reduces and cleans up the code, especially when you have 20 on a page:

myPromise.catch(logError).then((res) => {
    console.log(res);
});

But this has unintended behavior because the "then" is always called even when a reject() is fired.

test('a')
    .catch(err => {console.log('error ' + err.message)})
    .then(res => { console.log(res) });

function test(item) {
    return new Promise((resolve, reject) => {
        if (typeof item === 'number') {
            resolve(item);
        } else {
            reject(new Error('item not number'));
        }
    });
}

which yields:
error item not number
undefined

The "undefined" means the "then" was called. Is it crazy to put the catch before the then? Is there another way to accomplish this without putting the catch last? Thanks.

@spion
Copy link
Collaborator

spion commented Oct 1, 2016

.catch transforms errors into non-errors.

You want #1220

@spion spion closed this as completed Oct 1, 2016
@wayofthefuture
Copy link
Author

Would ".tap" work for what I'm trying to do? Thanks.

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

2 participants