Watch 10 video solutions for Convert Callback Based Function to Promise Based Function, a medium level problem. This walkthrough by Web Dev Simplified has 366,885 views views. Want to try solving it yourself? Practice on FleetCode or read the detailed text solution.
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;
}
}
Example 1:
Input:
fn = (callback, a, b, c) => {
callback(a * b * c);
}
args = [1, 2, 3]
Output: {"resolved": 6}
Explanation:
const asyncFunc = promisify(fn);
asyncFunc(1, 2, 3).then(console.log); // 6
fn is called with a callback as the first argument and args as the rest. The promise based version of fn resolves a value of 6 when called with (1, 2, 3).
Example 2:
Input:
fn = (callback, a, b, c) => {
callback(a * b * c, "Promise Rejected");
}
args = [4, 5, 6]
Output: {"rejected": "Promise Rejected"}
Explanation:
const asyncFunc = promisify(fn);
asyncFunc(4, 5, 6).catch(console.log); // "Promise Rejected"
fn is called with a callback as the first argument and args as the rest. As the second argument, the callback accepts an error message, so when fn is called, the promise is rejected with a error message provided in the callback. Note that it did not matter what was passed as the first argument into the callback.
Constraints:
1 <= args.length <= 1000 <= args[i] <= 104Problem Overview: You are given a function that follows the classic callback pattern where the last argument is a callback. The task is to create a wrapper that converts this function into a Promise-based version so callers can use async/await or .then() instead of callbacks.
Approach 1: Promise Wrapper Around Callback (O(1) time, O(1) space)
The simplest way is to return a new function that wraps the original function call inside a Promise. The wrapper collects all arguments using rest parameters (...args). When the wrapper runs, it creates a new Promise and calls the original function with the provided arguments plus a callback that resolves the promise. Once the callback executes, it passes the result to resolve, completing the asynchronous flow.
The key insight is that a Promise represents a future value. By resolving the Promise inside the callback, you bridge the old callback style with modern Promise-based control flow. This pattern is essentially a simplified version of Node's util.promisify. Time complexity is O(1) because the wrapper only performs a constant number of operations regardless of input size. Space complexity is also O(1), since it only allocates a single Promise and callback.
This approach is common when migrating legacy JavaScript APIs to modern asynchronous patterns. It works well with JavaScript runtime environments that rely heavily on callbacks. Once converted, the function becomes compatible with async programming patterns and integrates naturally with Promises and async/await.
Approach 2: Generic Promisify Wrapper with Error Handling (O(1) time, O(1) space)
Some callback APIs follow the Node.js error-first convention where the callback receives (error, result). A more defensive wrapper resolves when the result is returned and rejects when an error is provided. Inside the Promise executor, the wrapper injects a callback that checks whether the error argument is present. If so, it calls reject(error); otherwise it calls resolve(result).
This version is slightly more robust because it handles both success and failure paths. The operational complexity remains constant since it only performs a simple conditional check before resolving or rejecting the Promise.
Recommended for interviews: Interviewers typically expect the Promise wrapper approach. The critical signal is recognizing that you can inject a callback that triggers resolve. Demonstrating the Node-style error handling variant shows deeper understanding of real-world asynchronous APIs.
| Approach | Time | Space | When to Use |
|---|---|---|---|
| Basic Promise Wrapper | O(1) | O(1) | When the callback simply returns a result without error handling |
| Error-First Promisify Wrapper | O(1) | O(1) | When converting Node.js-style callbacks that use (error, result) |