Files
30-seconds-of-code/snippets/promisify.md
2017-12-13 10:33:39 +00:00

495 B

Promisify

Creates a promisified version of the given callback-style function. 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 stat = promisify(fs.stat)
// stat('foo.txt') -> Promise resolves if `foo.txt` exists, otherwise rejects