---
title: "How to convert callback-based code to use Promises?"  
description: "How to convert callback-based code to use Promises?"  
author: "Sandra Emily"  
published: 2023-09-26  
updated: 2023-09-26  
canonical: https://www.mindstick.com/forum/159920/how-to-convert-callback-based-code-to-use-promises  
category: "javascript"  
tags: ["javascript", "promise"]  
reading_time: 2 minutes  

---

# How to convert callback-based code to use Promises?

How to [convert](https://www.mindstick.com/forum/2093/configurationmanager-appsettings-convert-n-to-n-why) [callback](https://www.mindstick.com/articles/152/using-the-callback)-based [code](https://yourviews.mindstick.com/view/85458/alan-turing-the-mastermind-behind-cracking-the-enigma-code-during-world-war-ii) to use [Promises](https://www.mindstick.com/forum/160100/how-do-promises-in-javascript-simplify-asynchronous-code-compared-to-callbacks)?

## Replies

### Reply by Aryan Kumar

Converting callback-based code to use Promises in JavaScript involves wrapping the asynchronous operations that use callbacks into functions that return Promises. This allows you to work with Promises and take advantage of their features like chaining, error handling, and improved readability. Here's a step-by-step guide on how to do this:

## Step 1: Identify the Asynchronous Function with a Callback

Identify the function that performs the asynchronous operation using a callback. For example, you might have a function like this:

```plaintext
function fetchDataFromServer(callback) {
  // Asynchronous operation
  setTimeout(() => {
    const data = { message: "Data received from the server" };
    callback(data, null); // Pass data as the result and null as the error
  }, 1000);
}
```

## Step 2: Wrap the Function with a Promise

Create a new function that wraps the callback-based function with a Promise. Inside the Promise, invoke the original function and resolve or reject the Promise based on the callback result.

```plaintext
function fetchDataFromServerWithPromise() {
  return new Promise((resolve, reject) => {
    fetchDataFromServer((data, error) => {
      if (error) {
        reject(error); // Reject the Promise with the error
      } else {
        resolve(data); // Resolve the Promise with the result
      }
    });
  });
}
```

## Step 3: Use the Promise-Based Function

Now you can use the **fetchDataFromServerWithPromise** function, which returns a Promise, and leverage the benefits of Promises such as **.then()** and **.catch()** for handling success and errors.

```plaintext
fetchDataFromServerWithPromise()
  .then((data) => {
    console.log("Data:", data);
  })
  .catch((error) => {
    console.error("Error:", error);
  });
```

By converting callback-based code to use Promises in this way, you make your code more readable, maintainable, and easier to work with, especially when dealing with complex asynchronous operations and error handling.


---

Original Source: https://www.mindstick.com/forum/159920/how-to-convert-callback-based-code-to-use-promises

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
