Skip to content
Turadg Aleahmad edited this page Apr 4, 2023 · 2 revisions

Rule

Use E.when(promise, onfulfilled) instead of promise.then(onfulfilled).

Explanation

E.when produces tracking info necessary for our deep stacks. .then , .catch, .finally, and await do not.

Keep in mind that the onrejected param to E.when handles only rejection of promise argument. If you want a single rejection handler like the Promise provides,

promise.then(onfulfilled).catch(onrejected)

then use the promise returned by E.when,

E.when(p, onfulfilled).catch(onrejected)

Note that E.when(p, onfulfilled).catch(onrejected) still loses tracking info compared to

E.when(
  E.when(p, onfulfilled), 
  undefined, 
  onrejected
)

Bear in mind that handler of the primary promise may reject as well. These will catch rejections in onFulfilled:

p.then(onFulfilled).catch(onRejected);
E.when(
  E.when(p, onfulfilled), 
  undefined, 
  onrejected
);

…and these won't:

p.then(onFulfilled, onRejected);
E.when(p, onFulfilled, onRejected);
Clone this wiki locally