ES6
September 10, 2026 3 min read

JavaScript Promise, Async and Await

HS
HEMANT SINGH
Technical Writer & Educator

JavaScript Promise, Async and Await

In JavaScript, Promise, async, and await are used to handle asynchronous operations.

For example, when we send data from JavaScript to a Laravel server, the server may take some time to respond. JavaScript can continue doing other work while waiting for the response.

Let's understand these concepts with a simple example.

1. What is a Promise?

A Promise is an object that represents the result of an asynchronous operation.

In simple words:

A Promise means: "I will give you the result later."

For example:

let promise = new Promise((resolve, reject) => {

setTimeout(() => {
resolve("Blog saved successfully");

}, 2000);

});

Here, the Promise takes 2 seconds to complete.

Initially, the Promise is:

Pending

After 2 seconds, it becomes:

Fulfilled

because we called:

resolve("Blog saved successfully");

2. Promise States

A Promise has three main states:

Pending

The operation is still running.

Pending

Fulfilled

The operation was successful.

Fulfilled

Rejected

The operation failed.

Rejected

For example:

let promise = new Promise((resolve, reject) => {

let success = true;

if (success) {
resolve("Blog saved successfully");
} else {
reject("Something went wrong");
}

});

If success is true, the Promise is fulfilled.

If success is false, the Promise is rejected.

3. Using a Promise with .then()

We can get the result of a Promise using .then().

let promise = new Promise((resolve, reject) => {

setTimeout(() => {
resolve("Blog saved successfully");

}, 2000);

});

promise.then((message) => {

console.log(message);

});

After 2 seconds, the output will be:

Blog saved successfully

The .then() function runs when the Promise is successfully completed.

4. What is async?

The async keyword is used before a function.

async function publishBlog() {

console.log("Publishing blog...");

}

An async function always returns a Promise.

For example:

async function publishBlog() {

return "Blog published successfully";

}

When we call:

let result = publishBlog();

console.log(result);

The result is a Promise.

Promise

To get the actual value, we can use .then():

publishBlog().then((message) => {

console.log(message);

});

Output:

Blog published successfully

5. What is await?

The await keyword is used to wait for a Promise to complete.

For example:

function saveBlog() {

return new Promise((resolve) => {

setTimeout(() => {

resolve("Blog saved successfully");

}, 2000);

});

}

Now we can use await:

async function publishBlog() {

let message = await saveBlog();

console.log(message);

}

publishBlog();

Here:

let message = await saveBlog();

means:

Wait for the Promise returned by saveBlog() to complete.

After 2 seconds:

Blog saved successfully

6. Why do we need async and await?

Without async/await, we might use .then():

saveBlog().then((message) => {

console.log(message);

});

With async/await:

async function publishBlog() {

let message = await saveBlog();

console.log(message);

}

publishBlog();

The second version is often easier to read.

7. Simple Real-Life Example

Imagine you are ordering food.

You place an order:

Order Food
↓
Wait for food
↓
Food is ready

A Promise is like saying:

"I promise that I will give you the food when it is ready."

async means:

"This function can perform asynchronous work."

await means:

"Wait until the food is ready."

8. Complete Example

Let's combine Promise, async, and await.

function saveBlog() {

return new Promise((resolve, reject) => {

setTimeout(() => {

resolve("Blog saved successfully");

}, 2000);

});

}


async function publishBlog() {

console.log("Saving blog...");

let message = await saveBlog();

console.log(message);

}


publishBlog();

Output:

Saving blog...

After 2 seconds:

Blog saved successfully

9. Promise + async + await Together

Remember this simple structure:

function getData() {

return new Promise((resolve, reject) => {

// asynchronous operation

resolve("Data received");

});

}


async function main() {

let data = await getData();

console.log(data);

}


main();

Here:

new Promise()

creates a Promise.

async function main()

creates an asynchronous function.

await getData()

waits for the Promise to complete.