Skip to content

JavaScript Promises and Callbacks

Posted on:July 26, 2019Β atΒ 04:18 PM

I really enjoy using JavaScript Promises for asynchronous operations. I find the API to be clear and easy to understand. Example:

dispatchAsyncProcess()
  .then(response => {
    // Success callback πŸŽ‰
  })
  .catch(error => {
    // Error callback 😟
  })
  .finally(() => {
    //release resources / Stop loader etc πŸ›‘
  });

Life is not always so straight forward though! Chances are, we do not always need all the available callbacks. So what are the rules around which callbacks are executed and when?

The gist

Armed with this knowledge, we can confidently work with promises in our code.