Javascript: No More callbacks use promisify to convert callback to promise
Tags: javascript, promise, callback, webdev
Published: 2022-11-20 05:33:00

Post: [Javascript: No More callbacks use promisify to convert callback to promise — Rahul Sharma(DevsMitra)](https://devsmitra.blogspot.com/2022/11/javascript-no-more-callbacks-use.html)

### What are callback and promise?

The callback is a function that is passed as a parameter to another function. And this function will be called inside the function.

[![Javascript callback function for beginners | DevsMitra](https://img.youtube.com/vi/Z0-2q2koJqs/0.jpg)](https://www.youtube.com/watch?v=Z0-2q2koJqs)

A promise is an object which is used to handle asynchronous operations. It is used to handle multiple asynchronous operations which may have a dependency on each other.

[![Javascript promises in depth | DevsMitra](https://img.youtube.com/vi/aZfVnrdrxoc/0.jpg)](https://youtu.be/aZfVnrdrxoc?autoplay=1)

---

### Why do we need a callback and promise?

Callback and promise are used to handle asynchronous operations. An asynchronous operation is an operation that is not executed immediately. It is executed after some time. For example, if you are making a request to the server to get some data, then it will take some time to get a response from the server. So, we need to wait for the response. If we are making multiple requests to the server, then we need to wait for all the responses. So, we need to handle asynchronous operations.

---

### Why do we need to convert callback to promise?

We need to convert callback to promise because the promise is more readable and easy to handle. We can handle multiple asynchronous operations using promise. We can also handle asynchronous operations which have a dependency on each other using promise.

Let's understand this with an example.

Suppose we have a simple add function that will take two numbers and return the sum of those two numbers in a callback function. I've also added the setTimeout function to simulate the asynchronous behavior of the function.

Note: I'm creating a standard error first callback function. In the standard error first callback function, the first parameter is an error, and the second parameter results.

```js
function add(a, b, callback) {
  setTimeout(() => callback(null, a + b), 100);
}

add(1, 2, (err, sum) => {
  console.log(sum);
});
```

This is a pretty simple function. We are getting the result after 100ms. Now, let's say we want to add another number to the result. We can do this by passing the result to another function.

```js
add(1, 2, (err, first) => {
  console.log(first);
  add(first, 3, (err, second) => {
    console.log(second);
    add(second, 4, (err, finalResult) => {
      console.log(finalResult);
    });
  });
});
```

If we use a callback function, then we need to write a nested callback function to handle asynchronous operations. This is not readable and it gets dipper and dipper as we add more asynchronous operations. This is also called callback hell.

---

Now we know the problem with a callback function. Let's see how we can solve this problem using promise.

### How to convert the callback function to a promise?

We will create a promisify function that will take the callback function as a parameter and return the promise. There are many npm packages available that can be used to convert the callback function to promise. But, I will show you how to convert the callback function to promise without using any npm package. Node.js already provides a utility function called promisify which can be used to convert the callback function to promise.

```js
const promisify =
  (fn) =>
  (...args) =>
    new Promise((resolve, reject) => {
      fn(...args, (err, result) => {
        if (err) {
          reject(err);
        } else {
          resolve(result);
        }
      });
    });
```

Let's see how it works.

```js
function add(a, b, callback) {
  setTimeout(() => callback(null, a + b), 100);
}

const addPromise = promisify(add);

const run = async () => {
  const first = await addPromise(1, 2);
  console.log(first);
  const second = await addPromise(first, 3);
  console.log(second);
  const finalResult = await addPromise(second, 4);
  console.log(finalResult);
};
```

This is much more readable and easy to handle. We can also handle multiple asynchronous operations which have a dependency on each other using promise.

If you are using node.js version 8 or above, then you can use util.promisify function to convert the callback function to promise.

```js
const { promisify } = require("util");
const addPromise = promisify(add);
```

---

Thank you for reading 😊

Got any additional questions? please leave a comment.

---

###### More content at [Dev.to](https://dev.to/devsmitra "Rahul Sharma(DevsMitra)").

{% user devsmitra %}

###### Catch me on
<p>
{% cta https://www.youtube.com/@devsmitra?sub_confirmation=1 %} Youtube {% endcta %}
{% cta https://github.com/devsmitra %} Github {% endcta %}
{% cta https://www.linkedin.com/in/rahul-sharma-18b8b374 %} LinkedIn {% endcta %}
{% cta https://devsmitra.medium.com %} Medium {% endcta %}
{% cta https://stackblitz.com/@devsmitra %} Stackblitz {% endcta %} {% cta https://devsmitra.hashnode.dev %} Hashnode {% endcta %}
{% cta https://hackernoon.com/u/devsmitra %} HackerNoon {% endcta %}
</p>