Return a promise from a callback-as-last-argument function using native a Promise, but only if native Promises exist.
Last updated 5 years ago by henrikjoreteg .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install native-promisify-if-present 
SYNC missed versions from official npm registry.


Conditionally returns a promise from a callback-as-last-argument function using a native Promise, but only if native Promises exist, and you didn't pass in a callback.

Otherwise your callback function works as usual.

Why another promisify module?! Sorry. Here's why:

  1. I'm writing a library and want to support both callbacks and promises
  2. I don't want to pick what Promise library you use if you're polyfilling it
  3. Since Promises are part of the web now, you should be polyfilling them and using native if present anyway, so this will look for Promises on window or global if defined.
  4. I still don't want it to cause JS errors if you were using the libarary I was building, needing to support IE, and you just want the callback API (even IE 11 doesn't have native promises).


npm install native-promisify-if-present


var promisify = require('native-promisify-if-present');

// write your callback function as usual
function someFunctionThatTakesCallback(name, callback) {
	// if we choose to do this it won't
	// blow up even if there's no native Promises
	// and no callback was passed
	if (!callback) {
		callback = function () {}

	// just error-first your callbacks as usual
	if (somethingWentWrong) {
		callback(new Error('oh crap'))
	} else {
		// error is `null` when successful
		callback(null, 'all is well')	

// call promisify to support both mechanisms
module.exports = promisify(someFunctionThatTakesCallback)


If you like this follow @HenrikJoreteg on twitter.



Current Tags

  • 1.0.1                                ...           latest (5 years ago)

2 Versions

  • 1.0.1                                ...           5 years ago
  • 1.0.0                                ...           5 years ago
Maintainers (1)
Today 1
This Week 47
This Month 509
Last Day 24
Last Week 145
Last Month 476
Dependencies (1)
Dev Dependencies (1)

Copyright 2014 - 2016 © |