Before Promises/async–await were common, many JS APIs used a callback-last, error-first style:
(…args, callback), where callback has the signature (err, value).
Implement promisify(fn) which:
fn that expects its final argument to be (err, value) => void.null or undefined error), the Promise resolves with the value.this context when the returned function is called (i.e., use fn.call(this,…)).fn throws synchronously, the Promise should reject with that error.Example
// Callback-last, error-first function function foo(url, options, callback) { fakeRequest(url, options) .then((data) => callback(null, data)) .catch((err) => callback(err)); } const promisifiedFoo = promisify(foo); // Now usable with async/await: const data = await promisifiedFoo("example.com", { q: 1 });
JavaScript Function
No test results yet
Click "Run" to execute tests