Skip to content

when you want to return a promise but need to provide a callback to some api

License

Notifications You must be signed in to change notification settings

jamestalmage/promise-shim

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

promise-shim Build Status

For when your promise libraries promisify method just won't cut it!

Promises are awesome, but sometimes you need to use API's that just do not support them. Most promise libraries provide a promisify method to convert API's that take node style callback, but they do not work in every situation. promise-shim helps fill the gap.

Install

$ npm install --save promise-shim

Usage

// You need to provide a Promise implementation:
var promiseShim = require('promise-shim')(PromiseConstructor);

// or if global.Promise is already a valid promise constructor:
var promiseShim = require('promise-shim')();

var promise = promiseShim(function (cb) {
  cb(someError); // rejects the promise
  cb(null, someValue); // resolves the promise
});

You can pass arguments to your function. Really helpful when using bind:

var myService = promiseShim.bind(null, function(cb, a, b) {
  console.log(a); // => 'foo-a';
  console.log(b); // => 'foo-b';
  cb(null, a + b);
});

// myService is now a reusable function that returns a promise:

myService('foo-a', 'foo-b').then(...);

Note: Always check your Promise libraries options for "promisification", you will only need promise-shim in specific instances.

Related:

License

MIT © James Talmage

About

when you want to return a promise but need to provide a callback to some api

Resources

License

Stars

Watchers

Forks

Packages

No packages published