Baz
Baz

Reputation: 13125

How to write nested Promises

Consider this code where start, continue and finish are promises.

export const do = () => {
    return new Promise((resolve, reject) => {
        start()
            .then(() => continue())
            .then(() => finish())
            .then(() => resolve())
            .catch((reason) => reject(reason))
    });
};

Is this how to write nested promises?

Upvotes: 1

Views: 91

Answers (1)

georg
georg

Reputation: 214949

Just return the whole chain, no need to wrap it:

export const _do = () => start()
            .then(continue)
            .then(finish)
;

Upvotes: 1

Related Questions