DEV Community

Discussion on: All (or just most) of what you need to know about handling Promises

Collapse
 
orivolfo profile image
Ori Volfovitch • Edited

Not sure what you mean.
You just removed the .catch(), which means you do not handle in case of rejection.

Collapse
 
karataev profile image
Eugene Karataev

Yeah, as Joel mentioned, you can return promises to get one flat promise chain.
Imagine that you have 4 promises. If you follow example 1 in your post, you'll get promise structure like callback hell, but if you return next promise in every then, you'll get nice and flat promise chain.

Thread Thread
 
orivolfo profile image
Ori Volfovitch • Edited

Oh, now I understand.
Wow, this is worth editing the original post!

Thanks!

Thread Thread
 
efishtain profile image
efi shtain

Another improvement you can make is not using anonymous function
It gives you testability and readability
So instead of:

longPromise()
.then((data)=>{
console.log(data); // logs: longPromise resolved
return shortPromise();
.then((data)=>{
console.log(data) // logs: shortPromise resolved
})
.catch((error)=> {
console.log(error);
})
});

you can do:
async function firstHandler(data){
console.log(data)
return shortPromise()
}

longPromise.then(firstHandler).then(console.log).catch(console.log)

Thread Thread
 
orivolfo profile image
Ori Volfovitch

But wouldn't this example start to get ugly if you had more that just 2 Promises?

Thread Thread
 
efishtain profile image
efi shtain

getData()
.then(processData)
.then(convertData)
.then(saveData)
.then(notifyOnSuccess)
.catch(handleError)

looks ok to me
any how I'd go with async await today as much as I can, only use native promises if I need to wrap a callback from a library or something like that

Thread Thread
 
orivolfo profile image
Ori Volfovitch

Nice combo.
So you are actually chaining them asynchronously?

Thread Thread
 
efishtain profile image
efi shtain

what do you mean?
you can't processData before you got the data, so it has to be done in series
getData() has to return a promise, the rest of the functions would automatically be converted to async

Thread Thread
 
orivolfo profile image
Ori Volfovitch • Edited

So why in your firstHandler example did you made the function to be async?

Thread Thread
 
efishtain profile image
efi shtain

Optional.
If you use it elsewhere, and it is async, mark it as async.
only when I use anonymous function directly in the chain I won't mark methods with async unless I have to.

Collapse
 
joetex profile image
Joel Ruiz • Edited

You can chain the Promises. Inside of .then(), you can return another promise and it'll let you add another .then() underneath. You add a single .catch() at very end, which will work for all the .then() chains.

longPromise()
    .then((data)=>{
        console.log(data); // logs: longPromise resolved
        return shortPromise();
    .then((data)=>{
        console.log(data) // logs: shortPromise resolved
    })
    .catch((error)=> {
         console.log(error);
    })
});