1

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?

Erick Petrucelli
  • 14,386
  • 8
  • 64
  • 84
Baz
  • 12,713
  • 38
  • 145
  • 268
  • 3
    Uhm, yes, that will work. But simply `do = () => start().then(continue).then(finish)` would work too, since that's already a promise and you don't need a `new Promise`. – deceze Nov 17 '16 at 09:54
  • 2
    Wouldn't class this as a duplicate personally, but it's relevant: [What is the explicit promise construction antipattern and how do I avoid it?](http://stackoverflow.com/questions/23803743/what-is-the-explicit-promise-construction-antipattern-and-how-do-i-avoid-it) – Joe Clay Nov 17 '16 at 09:57

1 Answers1

1

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

export const _do = () => start()
            .then(continue)
            .then(finish)
;
georg
  • 211,518
  • 52
  • 313
  • 390