Javascript Async/Await/Microtask Queue Explained

const multiply = (num1, num2) =>
new Promise((resolve) => resolve(num1 * num2))
const sum = (total, num3) =>
new Promise((resolve) => resolve(total + num3))
const multiplyThenSumAsync = async (num1, num2, num3) => {
const product = await multiply(num1, num2)
const sumTotal = await sum(product, num3)
return sumTotal
}
const getResult = () => (
multiply(10, 20)
.then(product => sum(product, 30))
.then(total => {
console.log('total using regular promise', total)
return total
})
)
const getResultAsync = async () => {
const product = await multiply(10, 20)
const total = await sum(product, 30)
console.log('total using async', total)
}
// Just for verification, We still love you callbacks :)
getResult().then(res =>
getResultAsync().then(resAsync =>
console.log('Same result => ', resAsync === res)
)
)

--

--

--

React enthusiast, AVID learner

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

10 things to know about React

My Introduction To Three.js

[Part 2] Get rapid offline capability in your Angular app with service worker

Improving Javascript Performance by Understanding V8

How Concurrent React changes the game for data-heavy UI

A Beginner’s JavaScript Study Plan

How to Improve Your Skills as a Developer

How to sort with Array.sort()

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Aldi Zhupani

Aldi Zhupani

React enthusiast, AVID learner

More from Medium

Application Framework with JavaScript

Garbage Collection in Javascript

How to Create a Linked List in JavaScript