What is an executor function for a Javascript promise?

We can pass an executor function to the constructor of a Javascript native promise. It takes two parameters: the resolve and rejects callouts of that promise:

new Promise( 
    () => {...} 
).then ( ... );

Based on our needs we can customize the behavior of that Promise using the exector.

For example, we can have for this executor function to automatically trigger the resolve or the rejects if our promise does not get an answer after some amount of time.

const executor = (resolve, reject) => {
    setTimeout(
        () => reject("Did not get an anwser after 1 sec")
    , 1000);
};

// this promise will automatically be rejected after 1 sec
new Promise(executor).then(result => {
    console.log(result);
});

More details about it here.

📖 50 Javascript, React and NextJs Projects

Learn by doing with this FREE ebook! Not sure what to build? Dive in with 50 projects with project briefs and wireframes! Choose from 8 project categories and get started right away.

📖 50 Javascript, React and NextJs Projects

Learn by doing with this FREE ebook! Not sure what to build? Dive in with 50 projects with project briefs and wireframes! Choose from 8 project categories and get started right away.


Leave a Reply

Your email address will not be published. Required fields are marked *

Home Screencasts Best of Newsletter Search X

📖 50 Javascript, React and NextJs Projects

Hi friend! Before you go, just wanted to let you know about the 50 Javascript, React and NextJs Projects FREE ebook.

One of the best ways to learn is by doing the work. Choose from 8 project categories and get started right away:

  • Business & Real-World
  • Games & Puzzles
  • Fun & Interesting
  • Tools & Libraries
  • Personal & Portfolio
  • Project Add-Ons
  • Productivity
  • Clones

Learn by doing with this FREE ebook! Not sure what to build? Dive in with 50 projects complete with project briefs and wireframes!

Keep building and level up! Get all projects as an ebook right to your inbox!

X