Pak Jiddat

Read: In the name of thy Lord Who createth, Createth man from a clot. Read: And thy Lord is the Most Bounteous, Who teacheth by the pen, Teacheth man that which he knew not. Nay, but verily man is rebellious That he thinketh himself independent!. Lo! unto thy Lord is the return. (Sura Alalaq 96:8)

Using await and async with Promises

Created On: 08 Nov, 2018: 11:49:18 - Tags : javascript

Url: https://javascript.info/async-await

The async keyword is used when declaring functions. A function declared with async must return a promise. If a function is declared with async and it does not return a promise, then the JavaScript engine will automatically convert the return value of the function to a promise object. For example the following functions are both valid async functions:


async function f() {
  return Promise.resolve(1);
}

async function f() {
  return 1;
}

The await keyword is used to wait for a promise to resolve. It causes code execution to wait until the promise has been resolved. The await keyword can only be used inside functions declared with async. It cannot be used inside regular functions. The following example shows how to use the await keyword:


async function Test() {

  let promise = new Promise((resolve, reject) => {
    setTimeout(() => resolve("done!"), 1000)
  });

  let result = await promise; // wait till the promise resolves

  alert(result); // "done!"
}

Test();


Related Articles

  1. ES6 brings new features to JavaScript
  2. Using Web Components in JavaScript
  3. Using JavaScript promises
  4. JavaScript arrow functions and the let keyword
  5. Using classes and objects in JavaScript
  6. Using Generators in JavaScript
  7. Difference between ECMAScript, TypeScript and JavaScript
  8. Adding event listeners using JavaScript
  9. Using Prefetch, Preload and Preconnect for speeding page load times
  10. Get current unix timestamp using Javascript
  11. Fetching parent node element of a HTML node
  12. Adding option to html selectbox using Javascript
  13. Fetching html elements using CSS class names
  14. Fetching html elements using CSS selectors with Javascript
  15. Changing CSS styles using Javascript
  16. Allowing Cross Origin Requests using htaccess
  17. NodeJs permission denied error
  18. Customizing Redmine layout
  19. Passing parameters to Jquery event handler function
  20. W3C.Data
  21. Making cross domain Ajax calls using JSONP
  22. Use of Preflight request in HTTP Cross Origin Resourse Sharing (CORS)
  23. Javascript JSON guide
  24. Using cookies in Javascript
  25. Convert javascript object to json string
  26. Javascript date object
  27. Get the time using JavaScript
  28. Base64 Encoding of utf-8 encoded string using Javascript
  29. Setting CSS styles using JQuery
  30. Replacing html element using replaceWith jquery function
  31. Using keydown event handler for detecting enter key press
  32. Replacing a string using Javascript replace function
  33. Using clearTimeout and onmouseenter, onemouseleave events
  34. Get co-ordinates of element relative to document using Jquery
  35. Iterate throught Javascript object properties
  36. Select contents on text field