JavaScript: Promise

A Promise represents asynchronous work that is pending, fulfilled with a value, or rejected with an error.

What you will learn

Minimal example

fetch('/api/items')
  .then(response => response.json())
  .then(items => showItems(items))
  .catch(error => showMessage('Could not load items.'))
  .finally(() => hideLoading());

Each then receives the previous result. Returning a Promise waits for that asynchronous step before the next handler runs.

Common mistakes