Asynchronous JavaScript

WHAT THEN IS ASYNC JS?

Code snippet for a synchronous program
Code snippet for an async program
Example of a callback function
  • Pending — Initial state when the promise is created.
  • Fulfilled — Which means the operation completed with success.
  • Rejected — Which means the operation failed.
Simple example of a promise structure.
promise()
.then(doSomething)
.then(doSomethingElse)
.then(doOneLastThing)
.catch()
Code snippet for an async/await program.
First message
Second message
Data back
Third message
const asyncFunction = async () => {
try {
const promise = new Promise((resolve, reject) => {
setTimeout(() => {
resolve("Data back");
}, 3000);
});
console.log("First message");
console.log("Second message");
console.log(await promise);
console.log("Third message");
} catch(error) {
console.log('error here')
}
};
asyncFunction();

--

--

--

Software Engineer

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

Recommended from Medium

Linked lists are something we’ve all heard of, and for good reason: They serve as a base…

Sequelize, associations & Jest

Improve Front end performance

https://youtu.be/fPO76Jlnz6c

Understanding React — Component life-cycle Methods(React 16.3+)

Eliminating Duplicates in Arrays

Drag and Drop In React- Part 1.

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
Swithin A. Okyere

Swithin A. Okyere

Software Engineer

More from Medium

Ternary operator in JavaScript

Javascript forEach Method

browser support image

Title Case in Javascript

Writing Cleaner Code With JavaScript Array Methods