"await" keyword within the function, we need to add "async" keyword in front of the function. The await operator is used to wait for a Promise. It can only be used inside an async function within regular JavaScript code.
Example
that the bcrypt.hash() function may take a while to generate the hash, and so we use the "await" keyword in front of it.
The await expression causes async function execution to pause until a Promise is settled and to resume execution of the async function after fulfillment. When resumed, the value of the await expression is that of the fulfilled Promise. If the Promise is rejected, the await expression throws the rejected value.
simply await the function that returns the Promise.
The await keyword is used inside the async function to wait for the asynchronous operation.
The use of await pauses the async function until the promise returns a result (resolve or reject) value.
Examples
Awaiting a promise to be fulfilled :-
If a Promise is passed to an await expression, it waits for the Promise to be fulfilled and returns the fulfilled value.
async function :-
async keyword with a function to represent that the function is an asynchronous function. The async function returns a promise.
Thenable objects :-
Thenable objects will be fulfilled just the same result.
Example: After async/await
The above code asks the javascript engine running the code to wait for the request.get() function to complete before moving on to the next line to execute it. The request.get() function returns a Promise for which user will await . Before async/await, if it needs to be made sure that the functions are running in the desired sequence, that is one after the another, chain them one after the another or register callbacks.
Promise
In JavaScript, a promise is a good way to handle asynchronous operations. It is used to find out if the asynchronous operation is successfully completed or not.
A promise may have one of three states.
A promise starts in a pending state. That means the process is not complete. If the operation is successful, the process ends in a fulfilled state. And, if an error occurs, the process ends in a rejected state.
For example, when you request data from the server by using a promise, it will be in a pending state. When the data arrives successfully, it will be in a fulfilled state. If an error occurs, then it will be in a rejected state.
Create a Promise
To create a promise object, we use the Promise() constructor.
The Promise() constructor takes a function as an argument. The function also accepts two functions resolve() and reject().
If the promise returns successfully, the resolve() function is called. And, if an error occurs, the reject() function is called.
Example:-
Promise Chaining
Promises are useful when you have to handle more than one asynchronous task, one after another.
You can perform an operation after a promise is resolved using methods then(), catch() and finally().
then() method
Example 2: Chaining the Promise with then()
JavaScript catch() method
The catch() method is used with the callback when the promise is rejected or if an error occurs.
JavaScript Promise Methods
Trending Tutorials