Frontend101 logoFrontend101

JavaScript Promisify: Convert Callbacks into Promises

Satish Shriwas·Oct 26, 2026·5 min read
Original on Medium ↗

Introduction

Before Promises and async/await became popular, JavaScript used callbacks to handle asynchronous operations.

For example, a function might call a callback when an API request finishes. However, callbacks can become difficult to manage when multiple asynchronous operations depend on each other.

JavaScript provides util.promisify in Node.js to convert callback-based functions into Promise-based functions.

In this article, we’ll understand how to implement our own promisify() function.

Photo by Chris Ried on Unsplash

1. Understanding the Problem

Consider this callback-based function:

function foo(url, options, callback) {
apiCall(url, options)
.then((data) => callback(null, data))
.catch((err) => callback(err));
}

The callback follows the error-first convention:

  • callback(null, data) means the operation succeeded.
  • callback(err) means the operation failed.

We want to convert this function so that we can use async/await instead of providing a callback manually.

const promisifiedFoo = promisify(foo);
const data = await promisifiedFoo(
'example.com',
{ foo: 1 }
);

Notice that we pass only the URL and options. The promisify() function automatically adds the callback.

2. Implementing Promisify

Here is the complete implementation:

export default function promisify(func) {
return function (...args) {
return new Promise((resolve, reject) => {
func.call(this, ...args, (err, result) => {
if (err) {
reject(err);
} else {
resolve(result);
}
});
});
};
}

Let’s understand how it works.

3. Step-by-Step Explanation

Step 1: Accept the original function

function promisify(func)

The func parameter represents the original callback-based function.

For example:

const promisifiedFoo = promisify(foo);

Here, func refers to foo.

Step 2: Return a new function

return function (...args) {

We return a new function that accepts any number of arguments.

The rest parameter ...args collects the arguments into an array.

For example:

promisifiedFoo('example.com', { foo: 1 });

The args array contains:

['example.com', { foo: 1 }]

We don’t need to specify the number of arguments in advance.

Step 3: Create a Promise

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

We create a Promise that will represent the result of the original asynchronous operation.

  • resolve(value) fulfills the Promise with a successful result.
  • reject(error) rejects the Promise when an error occurs.

These functions are provided automatically by the Promise constructor.

Step 4: Call the original function with a callback

func.call(this, ...args, (err, result) => {

This is the most important line in the implementation.

It does three things:

  1. Preserves the original call-time this value.
  2. Passes all the supplied arguments using ...args.
  3. Appends a new callback as the final argument.

For example, the call effectively becomes:

foo(
'example.com',
{ foo: 1 },
(err, result) => {
if (err) {
reject(err);
} else {
resolve(result);
}
}
);

The original function invokes this callback when its asynchronous operation completes.

Step 5: Handle success and failure

if (err) {
reject(err);
} else {
resolve(result);
}

The callback follows the error-first convention.

When the operation succeeds:

callback(null, 'API data');

The callback receives err = null and result = 'API data'. The implementation calls resolve('API data').

When the operation fails:

callback(new Error('Request failed'));

The callback receives an error, so the implementation calls reject(err).

This converts the callback result into a Promise result.

4. Dry Run

Let’s use a simpler example to see the complete flow.

function add(a, b, callback) {
setTimeout(() => {
callback(null, a + b);
}, 1000);
}
const promisifiedAdd = promisify(add);

Now call:

const result = await promisifiedAdd(2, 3);
console.log(result); // 5

Here’s the execution flow:

  1. promisify(add) returns a new function.
  2. promisifiedAdd(2, 3) collects 2 and 3 in args.
  3. A new Promise is created.
  4. The original function is called as add(2, 3, callback).
  5. After one second, add executes callback(null, 5).
  6. Our callback receives err = null and result = 5.
  7. resolve(5) fulfills the Promise.
  8. await returns 5, which is assigned to result.

The important point is that we don’t manually pass a callback when calling promisifiedAdd. The wrapper creates and passes it for us.

5. Why Preserve this?

Consider an object whose method uses this:

const calculator = {
value: 10,
getValue(callback) {
callback(null, this.value);
}
};

When the promisified method is called with the object as its receiver, we want this.value to remain 10.

That’s why we use:

func.call(this, ...args, callback);

Using call() preserves the receiver of the returned function while forwarding the arguments and appending the callback.

6. Common Mistake

A common mistake is to write:

Promise.resolve(func(...args));

This works with functions that return a value or a Promise, but it does not implement the required callback-based conversion.

The original function expects a callback as its final argument. We must supply that callback and use it to settle the Promise.

7. Time and Space Complexity

The wrapper performs constant additional work apart from invoking the original function and processing its callback.

  • Time complexity: O(n), where n is the number of supplied arguments, because the arguments are forwarded.
  • Auxiliary space complexity: O(n), because the arguments are collected into an array.

These bounds exclude the original asynchronous operation and the work it performs.