Javascript Promise Async Await – Very Simple Examples

Welcome to a quick tutorial and examples on how to use promise, async, and await in Javascript. So you have heard of asynchronous functions, and wondering how it works? Well, it is actually very simple.

  1. Asynchronous functions can be defined by prepending async to a function declaration. For example, async function demo () { return "FOO!"; }
  2. But since asynchronous functions do not wait for the processing to complete, it will return a promise. For example, var res = demo() will be a promise and not "FOO!".
  3. Lastly, we need to use then() to specify what to do when the processing is complete.
    • function done (result) { console.log(result); }
    • res.then(done);

That covers the quick basics, but let us walk through more examples in this guide – Read on!

ⓘ I have included a zip file with all the example source code at the start of this tutorial, so you don’t have to copy-paste everything… Or if you just want to dive straight in.

 

 

TABLE OF CONTENTS

Download & Notes Promise Async Await Useful Bits & Links
The End

 

DOWNLOAD & NOTES

Firstly, here is the download link to the example code as promised.

 

EXAMPLE CODE DOWNLOAD

Click here to download the source code, I have released it under the MIT license, so feel free to build on top of it or use it in your own project.

 

QUICK NOTES

If you spot a bug, please feel free to comment below. I try to answer questions too, but it is one person versus the entire world… If you need answers urgently, please check out my list of websites to get help with programming.

 

 

PROMISE ASYNC AWAIT

All right, let us now dive into the examples of using async, promise, and await in Javascript.

 

1) WORKING WITH ASYNC FUNCTIONS – THEN, CATCH, FINALLY

1-then-catch-finally.html
// (A) ASYNCHRONOUS FUNCTION
async function add (a, b) { return a+b; }
 
// (B) CALL ASYNC FUNCTION
add(11, 22)
 
// (C) THEN RESOLVE RESULT
.then (result => {
  console.log(result);
})
 
// (D) OPTIONAL - CATCH ERRORS
.catch(error => {
  console.log(error);
})
 
// (E) OPTIONAL - THIS WILL RUN REGARDLESS OF ERRORS
.finally(() => {
  console.log("Finally");
});

This may be a little intimidating for beginners, but keep calm and look carefully. This is pretty much just an “expanded version” of the introduction snippet.

  1. First, we declare our async function add().
  2. Call the asynchronous function.
  3. The then() block will run when the asynchronous function is successfully completed.
  4. The catch() block is optional and will run when errors occur.
  5. Lastly, the finally() block is also optional. It will run regardlessly, after then() or catch().

Yes, the then-catch-finally trio is something like try-catch-finally… But for handling asynchronous functions instead.

 

 

2) AWAIT

2-await.html
// (A) ASYNC MULTIPLY FUNCTION
async function multiply (first, second) {
  return first * second;
}
 
// (B) ASYNC CALC FUNCTION
async function calc () {
  var result = 23 + 5;
  result = await multiply (result, 3); // WAIT FOR COMPLETION
  return result;
}

// (C) RUN!
calc().then(res => {
  console.log(res); // 84
});

// (D) CAN ONLY USE AWAIT INSIDE ASYNC FUNCTIONS
// await multiply(5, 2); // ERROR!

Asynchronous functions do not wait for completion. So this pretty much a no-brainer, just use await to wait for the results from an async function. But please take note, await will only work inside another async function().

 

 

USEFUL BITS & LINKS

That’s all for the tutorial, and here is a small section on some extras and links that may be useful to you.

 

INFOGRAPHIC CHEAT SHEET

Javascript Promise & Await (Click to enlarge)

 

LINKS & REFERENCES

 

THE END

Thank you for reading, and we have come to the end. I hope that it has helped you to better understand, and if you want to share anything with this guide, please feel free to comment below. Good luck and happy coding!

Leave a Comment

Your email address will not be published. Required fields are marked *