1

I am learning ES6 promises, and want to confirm that I'm understanding the syntax. (If this works but there's a better way, I'd love to know that, too!)

Suppose I'm writing a function which returns a promise. (Let's say because I want to wrap a sequence of async().then().then().then()'s in a function.)

Question 1: Am I correct thinking that I need a function, wrapped in a then(), wrapped in a executor function, wrapped in a promise constructor, wrapped in the original function, like the below?

function iReturnAPromise() {
  //do some stuff here
  return new Promise(function(resolve, reject) {
    doAnAsyncThing()
      .then(...)
      .then(...)
      .then(function(results) {
        //resolve with another promise or reject with an error based on results
      });
  });
}

That amount of nesting seems crazy, and worse than the "callback hell/pyramid of doom" that promises are designed to fix. Am I overcomplicating (probably because I'm misunderstanding some part of promises) or is that really the syntax?

Question 2: Let's assume that really is the syntax. Are the following two snippets equivalent? Is one more idiomatic than the other?

a) Do a bunch of stuff before the return statement, and stick only the async thing inside the promise executor, like so:

function iReturnAPromise() {
  //do stuff here
  //do more stuff
  //do even more stuff
  return new Promise(function(resolve, reject) {
    doAnAsyncThing()
      .then(...)
      .then(...)
      .then(...);
  });
}

b) Wrap everything in the promise executor, like so:

function iReturnAPromise() {
  return new Promise(function(resolve, reject) {
    //do stuff here
    //do more stuff
    //do even more stuff
    doAnAsyncThing()
      .then(...)
      .then(...)
      .then(...);
  });
}
Katie Byers
  • 822
  • 8
  • 16
  • You don’t need the `new Promise` wrapper. Just `return doAnAsyncThing().then(`…`).then(`…`).then(`…`);`. – Sebastian Simon Nov 26 '17 at 17:01
  • *"Question 1: Am I correct thinking that I need a function, wrapped in a then(), wrapped in a executor function, wrapped in a promise constructor, wrapped in the original function, like the below?"* No, see the linked question's answers for details. :-) TL;DR: https://pastebin.com/vJmqUKu4 (Since you're new here: Just because it was marked as a duplicate, that doesn't mean it was a *bad* question.) – T.J. Crowder Nov 26 '17 at 17:01
  • 1
    Also look into [`async`/`await`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/async_function), a new feature as of the 2017 spec but supported in current versions of Chrome, Firefox, and Edge (and transpilable via Babel if needed). – T.J. Crowder Nov 26 '17 at 17:08
  • @T.J.Crowder - Thanks for the link to the other question! Hadn't known to call it that, so hadn't found it. And yes, await seems waaaaay more intuitive, but lots of folks seem very excited about promises, so I figured I should wrap my brain around them, before moving on to await! :-) – Katie Byers Nov 26 '17 at 17:12
  • @Katie: No worries. :-) Not a bad idea, esp. since `async`/`await` are just syntactic sugar around promises. – T.J. Crowder Nov 26 '17 at 17:30
  • @Katie "if there's a better way, I'd love to know that too" - async/await. Learn promises, but only because they are the foundation of async/await. It's the right abstraction – Sam H. Nov 26 '17 at 18:43

0 Answers0