E
E
Eugene2019-03-03 23:39:36
Node.js
Eugene, 2019-03-03 23:39:36

How to organize the structure of promises in asynchronous code?

I have a set of asynchronous functions that need to be executed one after the other. If one returns catch, the whole chain is interrupted. This is easy to do using a promise chain
.then().then().then().catch()
But for me it is important that each function has its own catch and leaves the chain execution in case of an error
The only option that comes to mind
is func.then( () => {
func2.then(() => { func3.then
(....).catch ()}).catch ()}).catch()
he is the embodiment of callback hell, which promises must solve

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexey Laud, 2019-03-03
@you_are_enot

asynchronous functions.
Instead of

something().then(() => {
    something2().then(() => {

    })
})

Write (inside an async function)
await something();
await something2();

await works on promises and any thenable objects (those that have a then method).
And all this can be wrapped in a try ... catch

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question