• Home
  • PHP
  • MySQL
  • Demos
  • HTML
  • CSS
  • jQuery
  • Framework
  • Social
  • Request Tutorial
PHP Lift
  • Home
  • Demos
  • Advertisement
PHP Lift
  • Home
  • PHP
  • MySQL
  • Demos
  • HTML
  • CSS
  • jQuery
  • Framework
  • Social
  • Request Tutorial
  • Follow
    • Facebook
    • Twitter
    • Google+
    • Pinterest
    • Youtube
    • Instagram
    • RSS
Understanding async-await in JavaScript
Home
JavaScript

Understanding async-await in JavaScript

February 1st, 2022 Huzoor Bux JavaScript, Tips 0 comments

Facebook Twitter Google+ LinkedIn Pinterest

JavaScript enabled Asynchronous programming to be easier to implement and understand. As a result, special Async and Await were introduced as keywords.

Let’s look at the reasons why they are employed.

Can utilize functions in various scenarios in which the execution of a function is contingent on the results of the previous function and needs to wait for it to be completed.

An easy illustration of this could be one of the most common examples is an API call. The information in the API call needs to be downloaded before performing other operations with it.

Async

An Async function is defined by including the Async keyword in the function. The function returns promises

It could be declared like this

const check = async () => {
      console.log("Async method")
}

Await

Await function is declared the same manner in the same way as the Async method by adding a keyword Await before the declaration of the function.

const check = await () => {
    console.log("Async method")
}

The word “await” must be declared in the Async function.

The wait keyword causes code execution to stop at the line where it is declared until the promise is completed. It does, however, stop it; the process of executing the code will continue on the line where it is declared. It doesn’t stop the execution of the code outside of the Async block.

Now that we can individually compose these tasks, let’s look at what we can accomplish using these functions.

const testMethod = async () => {
    const ret = await "JavaScript!"
    console.log(ret);
}

console.log("Before testMethod() call")
testMethod()
console.log("After testMethod() call")

Output

await async output

 

 

 

 

 

 

Share this:

  • Click to share on Twitter (Opens in new window)
  • Click to share on Facebook (Opens in new window)

Related

  • Tags
  • async-await
Facebook Twitter Google+ LinkedIn Pinterest
Next article Introduction to Git for version control
Previous article 7 Reasons PHP is Still So Important For Web Development

Huzoor Bux

I am a PHP Developer

Related Posts

3 reasons why you SHOULD write tests Guide
May 15th, 2022

3 reasons why you SHOULD write tests

How to create a screen recorder in JavaScript JavaScript
May 8th, 2022

How to create a screen recorder in JavaScript

Best 10 Programming Languages that will rule in 2022 Guide
May 1st, 2022

Best 10 Programming Languages that will rule in 2022

Leave a Reply Cancel reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

    Advertisement
    Like us
    Recent Posts
    • How to Extract Text from PDF using PHP
    • 3 reasons why you SHOULD write tests
    • How to create a screen recorder in JavaScript
    • Best 10 Programming Languages that will rule in 2022
    • Top 7 Websites To Get Your First Paid Internship
    Categories
    • API
    • Bootstrap
    • Bot
    • CSS
    • CSS 3
    • Database
    • Designing
    • Framework
    • Guide
    • HTML
    • HTML 5
    • JavaScript
    • jQuery
    • MySQL
    • Node.js
    • oAuth
    • Payment
    • PHP
    • Python
    • Social
    • Tips
    • WordPress
    Weekly Tags
    • PHP
    • How to
    • javascript
    • api
    • MYSQL
    • jQuery
    • PHP Basics
    • Programming Habits
    • HTML5
    • PHP framework
    • About
    • Privacy Policy
    • Back to top
    © PHPLift 2021. All rights reserved.
     

    Loading Comments...