Implement promisify(fn). fn uses the Node.js callback style. Its last parameter is a callback (err, value) => void.
Return a new function that takes the same arguments without the callback. The new function returns a promise.
- Call
fnwith the given arguments plus your own callback. Call it with the samethis. - If
erris truthy, reject witherr. Otherwise resolve withvalue, even whenvalueis falsy, such as0or''. - If
fnthrows synchronously, the promise rejects with that error. Synchronously means right away, during the call, instead of through the callback. - Don't use
util.promisify.
Input: const read = promisify((name, cb) => cb(null, 'hi ' + name));
await read('Ada')
Output: 'hi Ada'
Input: const fail = promisify(cb => cb(new Error('nope')));
await fail()
Output: rejects with Error('nope')