Last active
March 22, 2017 13:22
-
-
Save nfrasser/8a50b66224854938dfbf to your computer and use it in GitHub Desktop.
Promise helper method which can be used to convert any Node.js/io.js async function into a Promise. Written in ES6.
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
import {isArray} from 'lodash'; | |
import {Promise} from 'rsvp'; | |
/** | |
Make an RSVP promise out of a generic node function that takes a callback as | |
its last argument. | |
First argument is either (a) the function to call or (b) an array where the | |
first element is the context, and the second is the function to call. | |
Do not pass in the standard callback argument at the end | |
*/ | |
export default function promise(fn, ...args) { | |
var context = this; | |
if (isArray(fn)) { // can replace with `fn instanceof Array` | |
// context is first param, function is second | |
[context, fn] = fn; | |
} | |
if (typeof fn !== 'function') { | |
throw new Error("First argument for promise.make must be a function"); | |
} | |
// Return the promise | |
return new Promise((resolve, reject) => { | |
args.push((err, result) => { | |
if (err) { return reject(err); } | |
return resolve(result); | |
}); | |
fn.apply(context, args); | |
}); | |
} |
Shorter (but more basic) version:
// promise.js
export default (fn, ...args) => new Promise((resolve, reject) =>
fn(...args, (err, result) => err ? reject(err) : resolve(result))
);
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Usage: The first argument is always the function, and the rest are the parameters that function takes (minus the callback at the end).
If you need the function to use or retain a certain context, pass it in as an array of two elements.
e.g.,
promise([fs, fs.readFile], ...)
.