Rangle.io: React Training
gitbook
gitbook
  • Introduction
  • Setup
  • Functional JavaScript
  • ES6 constructs
    • Default Params
    • Template Literals
    • Destructuring
    • Arrow Functions
    • Promises
    • let and const
    • Modules
  • Thinking in Components
    • Atomic Design
    • Atomic Component Principles
    • Benefits of This Approach
    • The Process
    • Task #1
  • React Components
    • Stateless Components
    • Stateful Components
    • Stateful vs Stateless Components
    • Composition
    • Task #2
    • Task #3
    • Task #4
    • Task #5
  • Immutable
    • What Is Immutability?
    • The Case for Immutability
    • JavaScript Solutions
      • Object.assign
      • Object.freeze
    • Immutable.js Basics
      • Immutable.Map
        • Map.merge
      • Nested Objects
        • Deleting Keys
        • Maps are Iterable
      • Immutable.List
      • Performance
      • Persistent and Transient Data Structures
      • Official Documentation
    • Exercises
      • Task #1
      • Task #2
      • Task #3
      • Task #4
      • Task #5
      • Task #6
      • Task #7
  • Redux
    • Review of Reducers and Pure Functions
    • Redux Reducers
    • Redux Actions
    • Configuring your Application to use Redux
    • Using Redux with Components
    • Redux and Component Architecture
  • Routing
    • React Router
    • Router Redux
  • Forms
    • Redux Form
  • Testing
    • Setup
    • Components
    • Reducers
    • Actions
Powered by GitBook
On this page
  • Promises vs Callbacks
  • More on Promises
  • Catching Rejections

Was this helpful?

  1. ES6 constructs

Promises

PreviousArrow FunctionsNextlet and const

Last updated 6 years ago

Was this helpful?

Promises are built-in ES6.

const wait (ms) => {
  return new Promise((resolve, reject) => {
    setTimeout(resolve, ms);
  });
}

wait(1000).then(() => console.log('tick'));

Promises vs Callbacks

For HTTP Requests, our existing solution is to use callbacks:

request(url, (error, response) => {
  // handle success or error.
});
doSomethingElse();

A few problems exist with callbacks. One is known as . A larger problem is decomposition.

The callback pattern requires us to specify the task and the callback at the same time. In contrast, promises allow us to specify and dispatch the request in one place:

promise = fetch(url); //fetch is a replacement for XMLHttpRequest

and then to add the callback later, and in a different place:

promise.then(response => {
  // handle the response.
});

This also allows us to attach multiple handlers to the same task:

promise.then(response => {
  // handle the response.
});
promise.then(response => {
  // do something else with the response.
});

More on Promises

.then() always returns a promise. Always.

p1 = getDataAsync(query);

p2 = p1.then(
  results => transformData(results));

p2 is now a promise regardless of what transformData() returned. Even if something fails.

If the callback function returns a value, the promise resolves to that value:

p2 = p1.then(results => 1);

p2 will resolve to “1”.

If the callback function returns a promise, the promise resolves to a functionally equivalent promise:

p2 = p1.then(results => {
  let newPromise = getSomePromise();
  return newPromise;
});

p2 is now functionally equivalent to newPromise.

p2 = p1.then(
  results => throw Error('Oops'));

p2.then(results => {
  // You will be wondering why this is never
  // called.
});

p2 is still a promise, but now it will be rejected with the thrown error.

Why won't the second callback ever be called?

Catching Rejections

The function passed to then takes a second argument, i.e. error, which represents error catching within the promise chain.

fetch('http://ngcourse.herokuapp.com/api/v1/tasks')
  .then(response => response.data)
  .then(tasks => filterTasksAsynchronously(tasks))
  .then(tasks => {
    $log.info(tasks);
    vm.tasks = tasks;
  })
  .then(
    null, 
    error => log.error(error)
  );

Note that one catch at the end is often enough.

"Callback Hell"