Created
April 12, 2017 11:06
-
-
Save aeinbu/cbe1825337324bc2e80d5b96b970b0ac to your computer and use it in GitHub Desktop.
Function to turn node style functions with callbacks into promises. This is useful when using async/await, since promises can be awaited upon.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Function to turn node style functions with callbacks into promises. | |
// This is useful when using async/await, since promises can be awaited upon. | |
const callback = (resolve, reject) => (err, res) => err ? reject(err) : resolve(res); | |
const awaitable = async action => new Promise((resolve, reject) => action(callback(resolve, reject))) | |
module.exports = awaitable; | |
// Usage: | |
// const fs = require("fs"); | |
// const awaitable = require("./awaitable"); | |
// await awaitable(cb => fs.mkdir("new-folder", cb)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment