liveBook · Manning

Executor function

As explained in Secrets of the JavaScript Ninja, Second Edition

The function passed to a Promise constructor that runs immediately and receives resolve and reject callbacks.

An executor function is passed to the built-in Promise constructor. It runs immediately when the Promise is created and receives resolve and reject callbacks, which it calls to signal success or failure. section 6.3

Listing 6.10. Creating a simple promise
const ninjaPromise = new Promise((resolve, reject) => {      #A
  resolve("Hattori");                                        #B
  //reject("An error resolving a promise!");                 #B
});

ninjaPromise.then(ninja => {                                 #C
  assert(ninja === "Hattori", "We were promised Hattori!");  #C
}, err => {                                                  #D
  fail("There shouldn't be an error")                        #D
});                                                          #D

See an executor resolve a Promise with a value, followed by success and failure callbacks passed to then. section 6.3

How Secrets of the JavaScript Ninja, Second Edition covers it

How deep it goes
Defines the executor’s parameters and immediate execution, then demonstrates resolving a Promise and handling its outcome.
Examples
Demonstrates resolving with “Hattori” and compares server data fetching with the possibility of errors.
What you can do after
Build a simple Promise by passing an executor that calls resolve or reject.
How it connects
Connects executors to the Promise constructor, resolve and reject callbacks, asynchronous computation, and then.

This explainer was generated by Manning's knowledge graph from the text of Secrets of the JavaScript Ninja, Second Edition. The section links point to the parts of the book it draws on.