impatient-js icon indicating copy to clipboard operation
impatient-js copied to clipboard

Chapter: Promises for asynchronous programming

Open rauschma opened this issue 7 years ago • 5 comments
trafficstars

rauschma avatar Jul 19 '18 03:07 rauschma

  1. Typo in 37.1.4. States of promises
  • Once a Promise is settled, the settlement value (result or error) is cached. Thus, if .then() or .catch() are called after the settlement, they receive the ~chaed~ cached value.
  1. A mistake in 37.6.4. Nesting per se is not evil

In the following code, we benefit from ~not~ nesting:

db.open()
.then(connection => { // (A)
  return connection.select({ name: 'Jane' })
  .then(result => { // (B)
    // Process result
    // Use `connection` to make more queries
  })
  // ···
  .catch(error => {
    // handle errors
  })
  .finally(() => {
    connection.close(); // (C)
  });
})

But this code shows an example of good nesting.

VernonHawk avatar May 14 '19 16:05 VernonHawk

@VernonHawk Thanks! Both things will be fixed in the next release.

rauschma avatar May 19 '19 06:05 rauschma

“37.6.4 Not all nesting is bad” shows how nesting can be beneficial. The code sample uses finally(), which hasn't been discussed before, so it's not obvious what the code does.

The finally() method should probably be discussed earlier in the chapter.

For completeness sake, it might also make sense to mention the second (optional) parameter to then().

maksverver avatar Jul 23 '19 19:07 maksverver

In 37.1.1 It's mentioned, "A Promise-based function returns a Promise and sends it a result or an error (if and when it is done). The Promise passes it on to the relevant callbacks.", (But the promise based function 'addAsync()' only returns Promise and it is inside the promise the result or error is evaluated and passed on via resolve or reject, which is handled by call-backs of .then() or .catch().) The aforementioned statement in quotes is confusing. Can you please make it more clear by being more specific with an example.

Vishwa-code avatar Jul 02 '20 12:07 Vishwa-code

40.5.2.3 A simple implementation of Promise.all() function all(iterable) { ... const result = []; // (instead of let result;)

danzelito avatar Jan 06 '23 05:01 danzelito