Are JavaScript Promises Asynchronous?

Promises are the center of JavaScript's handling of asynchronus tasks. But promises are not asynchronous themselves.

They are associated to an asynchronous task and wait for it to finish. When that async task is over, they can run some pieces of code, but these are executed synchronously.

Let's see an example:

import axios from "axios";

const getUserPromise = axios("");
getUserPromise.then(function(jsonResponse) {
    console.log(`The name of user #230 is 
      ${} ${}`);

In this example, the axios function (part of the Axios library) performs an asynchronous task: it makes a request to a web service and returns a promise.

Once the call to the web service is complete, the promise will run the function with the console.log. However, that function will run synchronously.

If you want to know more about Promises and async, I recommend that you read this introductory article about the topic.

Become a Better JavaScript Developer

Easy, actionable steps to level up your JavaScript skills, right to your inbox. Enter your name and Email address below to subscribe: