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("https://fakewebservice.com/getUser/230");
getUserPromise.then(function(jsonResponse) {
    console.log(`The name of user #230 is 
      ${jsonResponse.data.firstName} ${jsonResponse.data.lastName}`);
});

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: