Sobes.tech
Middle

When are Promises initiated in JavaScript?

sobes.tech AI

Answer from AI

A Promise is initiated at the moment a new instance of the Promise class is created. The executor function, passed to the constructor, is called immediately by the JavaScript runtime, synchronously.

Example:

// This executor function will be called immediately upon creating the Promise
const myPromise = new Promise((resolve, reject) => {
  console.log('Executor function called');
  resolve('Success!');
});

console.log('Promise created');
// The output in the console will be:
// Executor function called
// Promise created
When are Promises initiated in JavaScript? — Node.js - sobes.tech