Promises
resolve, reject, then, catch, async, await.
Promise is in JavaScript is nothing but the CompletableFuture in Java.
This is used to start asynchronous processes and then we tell the executor about the functions that must be executed when the execution of this asynchronous process is completed.
resolve and reject
A Promise constructor takes one object - executor which is the function that must be executed in asynchronous mode and this executor must have two functions - resolve and reject as parameters.
Promise object structure is included in the JavaScript specification. Any implementation of JavaScript must have a constructor described as above.
These are private methods are executed by the engine when the executor function is successful or has failed. It then sets the internal properties of the Promise object to reflect the state of the execution and also stores the result object.
resolve and reject are system functions. These functions are named by the language specification and executed by any JavaScript engine which implements the Promise requirements correctly.
then and catch
After the promise is resolved or rejected, then the functions in then and catch are called with respective parameters.
The then and catch callbacks are automatically placed in the callback queue by the JavaScript engine as soon as the state of the Promise has changed.
async and awaits
Functions marked as async returns a Promise object.
Basically this is done without having to call new Promise()
constructor.
async function f() {
return 1; // can be anything. Need not be a real asynchronous function such as setTimeout() or fetch().
}
f().then(alert); // 1
await statement makes JavaScript wait until the promise is settled and returns its result. Basically this gets the resolved value from a Promise object without using a then.
We can use a regular try-catch block to get the rejected value from the Promise object.
async-await is more of a syntactic sugar to promise-then-catch. It makes the code more readable.
Promise.resolve() and Promise.reject()
These are static helper methods which return a Promise object but with the state and value of promise already updated.
This is useful certain cases where the method should always return a Promise but there is actually no task to perform. For example when we want to return something from a cache but the method's return type is always a Promise.
Always using the caching reference to understand the usage of the static helper methods.
- https://medium.com/@lydiahallie/javascript-visualized-promises-async-await-a3f1aad8a943
- https://stackoverflow.com/questions/31324110/why-does-the-promise-constructor-require-a-function-that-calls-resolve-when-co/31324439#31324439
- https://stackoverflow.com/questions/54723849/why-and-when-to-use-promise-resolve