Promise and Async/Await in JavaScript PDF
Document Details
Uploaded by AppreciatedChrysoprase495
Université Ferhat Abbas de Sétif
Dr. AISSAOUA HABIB
Tags
Summary
This document provides an explanation of promises and async/await in JavaScript. Promises are used for handling asynchronous operations, and async/await is a cleaner syntax for writing asynchronous code, making it easier to read and understand. It shows different ways to use and handle Promises and Async/await in JavaScript.
Full Transcript
Promise JavaScript provides a helper function Promise.all to handle multiple promises at once. In general, Promise.all takes an array of promises as an input and returns a Promise. Example: var allPromise = Promise.all([promise1, promise2,...]); Dr....
Promise JavaScript provides a helper function Promise.all to handle multiple promises at once. In general, Promise.all takes an array of promises as an input and returns a Promise. Example: var allPromise = Promise.all([promise1, promise2,...]); Dr. AISSAOUA HABIB University Setif -1- Promise The returned promise will resolve once all the promises passed as input are resolved. If the returned promise is resolved, the resolved values of all promises are returned as an array. Dr. AISSAOUA HABIB University Setif -1- Promise But if at least one promise rejects, then Promise.all() rejects right away (without waiting for other promises to resolve) In this example, the returned promise is rejected because the second promise is rejected. The catch() method is executed to display the reason for the rejected promise. Dr. AISSAOUA HABIB University Setif -1- Promise Promise.allSettled() works similarly to Promise.all() but instead of rejecting immediately if any promise rejects, it waits for all promises to settle and then returns an array of objects with the status and value/reason for each promise. Dr. AISSAOUA HABIB University Setif -1- Async/await If you don’t want to create a promise chain when dealing with asynchronous operations, then you can try async/await. We use the async keyword before the name of a function to represent that the function is an asynchronous function. When such a function or method is called, it returns a promise. Dr. AISSAOUA HABIB University Setif -1- Async/await The async keyword can be placed before a function, like this: async function f() { return 1; } ////// or async function f() { return Promise.resolve(1); } This function returns a resolved promise with the result of 1. Now, we can handle the function as follow: f().then((res)=>console.log(res)) Dr. AISSAOUA HABIB University Setif -1- Async/await The await keyword is used inside the async function to wait for the asynchronous operation. The syntax to use await is: let result = await promise; await makes JavaScript wait until that promise settles and returns its result. Dr. AISSAOUA HABIB University Setif -1- Async/await As result, we get a console log displaying done!. That means our code is executed within 5 second. The execution of the function gets paused at the (*) line for 5 second because we have set it to await. Dr. AISSAOUA HABIB University Setif -1-