@jaawerth/promisify
v1.0.2
Published
Simple, straightforward promisification for callback-based asynchronous functions.
Downloads
4
Readme
promisify
Simple, standalone promisification
This is a straightforward, no-frills implementation that will work on any asynchronous functions that take node-style callbacks.
You can always use the promisify that comes with Bluebird, but sometimes you may want to keep things native.
Install
npm install @jaawerth/promisify
Usage
var promisify = require('@jaawerth/promisify');
promisify(asyncCallbackFn[, thisArg]);
Use the global Promise constructor, the included shim, OR bring your own
Global Promise object (default)
const promisify = require('@jaawerth/promisify');
const readFile = promisify(require('fs').readFile);
readFile('./foo.txt', 'utf8').then(console.log);
Custom Promise constructor
Just make sure your Promise implementation adheres to the Promise constructor's API and behavior, and you can instantiate a promisify that will use it.
promisify = promisify.create(MyPromise);
Shim (uses es6-promise)
var promisify = require('@jaawerth/promisify/shim');
Keep the promisified function bound to its context as needed
var crabWalk = promisify(zoidberg.crabWalk, zoidberg);
crabWalk().then(runAway);
Or don't!
Just like any unbound function, you can manipulate context at will.
var promiseMe = promisify(function returnMe() {
return this;
});
promiseMe.call('No!'); // => Resolves to 'No!'