Skip to content

Home

Promisify function

Converts an asynchronous function to return a promise.

💬 Note

In Node 8+, you can use util.promisify.

const promisify = func => (...args) =>
  new Promise((resolve, reject) =>
    func(...args, (err, result) => (err ? reject(err) : resolve(result)))
  );

const delay = promisify((d, cb) => setTimeout(cb, d));
delay(2000).then(() => console.log('Hi!')); // Promise resolves after 2s

More like this

Start typing a keyphrase to see matching snippets.