Skip to main content
Back to problems
#2776
Medium JavaScript

Convert callback based function to promise based function

90.7% acceptance
Mar 31, 2026
19
12
Write a function that accepts another function fn and converts the callback-based function into a promise-based function. The function fn takes a callback as its first argument, along with any additional arguments args passed as separate inputs. The promisify function returns a new function that should return a promise. The promise should resolve with the argument passed as the first parameter of the callback when the callback is invoked without error, and reject with the error when the callback is called with an error as the second argument. The following is an example of a function that could be passed into promisify. function sum(callback, a, b) { if (a < 0 || b < 0) { const err = Error('a and b must be positive'); callback(undefined, err); } else { callback(a + b); } } This is the equivalent code based on promises: async function sum(a, b) { if (a < 0 || b < 0) { throw Error('a and b must be positive'); } else { return a + b; } }

Solution

TypeScript
Time O(1)
Space O(1)
LeetCode
solution.ts
type CallbackFn = (
  next: (data: number, error: string) => void,
  ...args: number[]
) => void;
type Promisified = (...args: number[]) => Promise<number>;

function promisify(fn: CallbackFn): Promisified {
  return function (...args) {
  return new Promise((resolve, reject) => {
    fn(
    (data, error) => {
      if (error) {
      reject(error);
      } else {
      resolve(data);
      }
    },
    ...args,
    );
  });
  };
}

/**
 * const asyncFunc = promisify(callback => callback(42));
 * asyncFunc().then(console.log); // 42
 */