Reputation: 3623
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