- What is the
resolve in the Promise constructor, and what resolve(x) do exactly?
The Promise constructor takes a callback function called executor. That executor callback will be called with two parameters. This call is made by the constructor itself. Both of parameters that will be passed in are functions:
resolve - when called will fulfill* the promise, marking it as successfully completed. The argument passed to the resolve function will become the value for that promise.
The value for the promise will be passed to .then() callback. If the promise is awaited the result is the value of the promise.
reject - when called will reject the promise, marking it as unsuccessfully completed. The argument passed to the reject function will become the reason that promise was rejected.
The reason for the rejection will be passed to .catch() callback. If the promise is awaited the reason is thrown and can be caught in a try/catch block.
Quick note: as with all parameters, these two can be given any name. The resolve and reject are very common ways of naming the functions.
* not necessarily true if passing another promise. If a another promise is passed, the current promise will adopt that promise's completion state. So, if resolve is called with a promise that is later rejected, the current promise will also be rejected.
The executor is then called immediately and the promise will be pending until either resolve or reject is called.
In your case the executor is:
resolve => {
setTimeout(() => {
resolve(x);
}, 2000);
}
Which means that resolve is going to be called 2 seconds later with the value for x making that the value for the promise.
The await will suspend the execution of f1() and resume it when promise returned by resolveAfter2Seconds(10) settles. The result of await resolveAfter2Seconds(10) is the value of the promise. Which is 10 as you can see in the log.
- What is asynchronous code? is it the call to
resolveAfter2Seconds or just the code inside resolveAfter2Seconds (that is: the promise execution, while resolveAfter2Seconds returns immediately?
I would say that for practical purposes, asynchronous code is anything that will finish later than the normal top-to-bottom execution of code:
console.log(1);
console.log(2);
console.log(3);
async function later(x) { return x; }
console.log(1);
later(2).then(value => console.log(value));
console.log(3);
You could definitely try to make an argument otherwise, however trying to pinpoint exactly where async code starts and ends is not very useful. It runs the risk of turning into something that resembles Sorites paradox. If fn1 is not async code, then is it calling fn2 which lies inside? Or is it fn3 which is inside fn2? Etc. Hence my proposed more practical solution. If a function causes some effect to come up later than the current run-to-completion cycle we can say that it is asynchronous. So, any function that returns a promise (which includes all async functions) are asynchronous.
You can also see my answer here which discusses various asynchronous functions more.
Further reading:
What is the correct terminology for javascript promises - very important as it explains the fundamental concept of promises.
`resolve` and `reject` functions inside a Promise's executor
How is a promise/defer library implemented?
Concept - Distilling how a promise works?