Live data from Hacker News

Mistakes we make using JavaScript Promises

betamark.com

1–10 of 60 posts

Re: Mistakes we make using JavaScript Promises

#3

And he makes mistake #1 in mistake #3 by running „get“ sequentially instead of using Promise.all

They're all artificial examples: one could assume that the tasks in #3 are intended to be executed serially. Though there aren't any data dependencies expressed between the different calls, perhaps ordering could be important for other reasons.

Re: Mistakes we make using JavaScript Promises

#4
post #3

And he makes mistake #1 in mistake #3 by running „get“ sequentially instead of using Promise.all

They're all artificial examples: one could assume that the tasks in #3 are intended to be executed serially. Though there aren't any data dependencies expressed between the different calls, perhaps ordering could be important for other reasons.

I feel it should have been called out explicitly when introducing await, because the 'clean' solution in async/await code is to call each async function and then await the results where you need them - which is a pattern he doesn't hint at at all.

Re: Mistakes we make using JavaScript Promises

#6
This "solution" given here has a syntax error since you don't have access to `user` within the second then() callback:

    get("http://data.com/user") 
      .then(user => get("http://data.com/location" + user.id))
      .then(location => createEntry(user, location)) 
      .then(response => {
        // handle response 
      }).catch(err => {
        // handle failure
      });
Instead, back before async/await made things easier this nested pattern was used (notice the difference in parenthesis location):

    get("http://data.com/user") 
      .then(user => get("http://data.com/location" + user.id)
        .then(location => createEntry(user, location)))
      .then(response => {
        // handle response 
      }).catch(err => {
        // handle failure
      });
Edit for completeness, this is how it is now with async/await:

    try {
      const user = await get("http://data.com/user"); 
      const location = await get("http://data.com/location" + user.id);
      const response = await createEntry(user, location);
      // handle response 
    } catch (error) {
      // handle failure
    }

Re: Mistakes we make using JavaScript Promises

#10
post #3

Earlier quoted context omitted.

They're all artificial examples: one could assume that the tasks in #3 are intended to be executed serially. Though there aren't any data dependencies expressed between the different calls, perhaps ordering could be important for other reasons.

I feel it should have been called out explicitly when introducing await, because the 'clean' solution in async/await code is to call each async function and then await the results where you need them - which is a pattern he doesn't hint at at all.

I think there is a danger in that approach: if you forget to await, errors are silently ignored. You can also await a Promise.all, which is reasonably good enough if you do depend on all of the results to do anything meaningful anyways.
Post reply on HN