nzakas / understandinges6

Content for the ebook "Understanding ECMAScript 6"
5.45k stars 796 forks source link

An important Promise rule isn't emphasized #382

Open tdd opened 7 years ago

tdd commented 7 years ago

Hi Nicholas,

Location: Chapter 11 > Page 230 > "Catching errors" > top code block and text below

There's an important rule in promises, which is that, because resolve and reject callbacks in a single then are never both called, the temptation to put all-catching code in the reject callback alongside a resolve callback is misleading: it wouldn't catch any error from its call-partner resolve callback.

Basically, it's best practice to always put reject callbacks alone on their "own step of the promise chain", be it with .then(null, handler) or the more palatable .catch(handler).

This tricks Promise beginners often enough that it is stressed in most promise tutorials, articles, workshops (including NodeSchool's promise-it-wont-hurt, for instance), so I figured you might want to illustrate this point?