Kid_Learning_C
Kid_Learning_C

Reputation: 3623

Javascript Promise: is the method name for resolve and reject arbitrary?

I am confused on the concepts of Promise, Resolve, Reject, then, catch.

While I know how to write code that works, I want to clarify some questions:

Question 1: Regarding Resolve, Reject, then, catch, Are these methods all introduced together with Promise? Before Promise is introduced to Javascript, does Javascript already have these special methods?

Question 2: Are the method names resolve, reject that are passed as parameters to executor functions in Promise arbitrary? For example:

const executorFunction = (resolve, reject) => {
  if ('1' === '1') {
      resolve('I resolved!');
  } else {
      reject('I rejected!'); 
  }
}
const myFirstPromise = new Promise(executorFunction);

This above code works well, no syntax error or anything. The promise will be in fulfilled state with 'I resolved!' value. But if I do:

const executorFunction = (A, B) => {
  if ('1' === '1') {
      A('I resolved!');
  } else {
      B('I rejected!'); 
  }
}
const myFirstPromise = new Promise(executorFunction);

Would it still work? Will the promise still be in fulfilled state with 'I resolved!' value?

And to make it further, if I swap the method names:

const executorFunction = (reject, resolve) => {
  if ('1' === '1') {
      reject('I resolved!');
  } else {
      resolve('I rejected!'); 
  }
}
const myFirstPromise = new Promise(executorFunction);

Now, would this work? Will the promise still be in fulfilled state with 'I resolved!' value?

Upvotes: 0

Views: 185

Answers (1)

SILENT
SILENT

Reputation: 4268

  1. Promise is an object spec that was included with Javascript. Before Promise was included as part of Javascript, the same spec could be built via code.
  2. Executor function is a function. Hence, it doesn't matter what the parameters are called, just their order.

Upvotes: 2

Related Questions