Skip to content

Instantly share code, notes, and snippets.

@aeinbu
Created April 12, 2017 11:06
Show Gist options
  • Save aeinbu/cbe1825337324bc2e80d5b96b970b0ac to your computer and use it in GitHub Desktop.
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.
// 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