Security News
Cloudflare Adds Security.txt Setup Wizard
Cloudflare has launched a setup wizard allowing users to easily create and manage a security.txt file for vulnerability disclosure on their websites.
promise-breaker
Advanced tools
Library to help write libraries that accept both promises and callbacks.
The promise-breaker npm package provides utilities for converting between callback-based functions and promise-based functions. It allows you to easily work with both styles of asynchronous code.
Callback to Promise
This feature allows you to convert a callback-based function into a promise-based function. The example demonstrates converting a simple callback function that adds two numbers into a promise-based function.
const promiseBreaker = require('promise-breaker');
function callbackFunction(arg1, arg2, callback) {
setTimeout(() => {
callback(null, arg1 + arg2);
}, 1000);
}
const promiseFunction = promiseBreaker.promisify(callbackFunction);
promiseFunction(1, 2).then(result => {
console.log(result); // 3
}).catch(err => {
console.error(err);
});
Promise to Callback
This feature allows you to convert a promise-based function into a callback-based function. The example demonstrates converting a simple promise function that adds two numbers into a callback-based function.
const promiseBreaker = require('promise-breaker');
function promiseFunction(arg1, arg2) {
return new Promise((resolve, reject) => {
setTimeout(() => {
resolve(arg1 + arg2);
}, 1000);
});
}
const callbackFunction = promiseBreaker.callbackify(promiseFunction);
callbackFunction(1, 2, (err, result) => {
if (err) {
console.error(err);
} else {
console.log(result); // 3
}
});
The util.promisify function in Node.js standard library provides similar functionality to promise-breaker for converting callback-based functions to promise-based functions. However, it does not provide the reverse functionality of converting promise-based functions to callback-based functions.
Bluebird is a fully-featured promise library that includes utilities for converting callback-based functions to promise-based functions (promisify) and vice versa (callbackify). It offers more features and better performance compared to promise-breaker, but it is a larger dependency.
promise-breaker
makes it easy to write functions that will accept an optional callback, or return
a Promise if a callback is not provided. You can use callbacks or Promises in your implementation,
and callers can call with either a callback or expect a Promise. It's a library that makes it easy
to write libraries for others.
npm install --save promise-breaker
This library assumes that Promise
is a defined global variable. If this is not the case
on your platform, you can use a polyfill:
npm install --save es6-promise
Then somewhere in your node.js application:
if(!global.Promise) {
global.Promise = require('es6-promise').Promise;
}
Or in your client-side app:
if(!window.Promise) {
window.Promise = require('es6-promise').Promise;
}
If you don't want to set the global, you can pass an optional Promise implementation to
promise-breaker
:
var MyPromise = require('es6-promise').Promise;
promiseBreaker = require('promise-breaker').withPromise(MyPromise);
With the growing popularity of Promises these days, if you're a library author, it's nice to be able to provide your clients with a library that will take an optional callback, and if the callback isn't provided, return a Promise. If you've ever tried to do this, you know that there's a lot of finicky boilerplate involved in every function you write. Providing callback support is also pretty important if you prefer to write your library using Promises internally.
'promise-breaker' makes this really easy. If you prefer writing in callback style:
// We're going to make some promises from callbacks
var pb = require('promise-breaker');
exports.myFunc = pb.make(function(done) {
done(null, "Hello World");
});
or if you prefer Promise style:
// We're going to break some promises down into callbacks
var pb = require('promise-breaker')
exports.myFunc = pb.break(function() {
return Promise.resolve("Hello World");
});
No matter which approach you take, users of your library can now call myFunc(done)
, or they
can call myFunc().then(...)
.
make(fn)
takes a function which accepts a callback(err, result)
as its last parameter, and
returns a new function which accepts an optional callback as its last parameter. If a callback is
provided, this new function will behave exactly like the original function. If the callback
is not provided, then the new function will return a Promise.
Note that pb.make()
uses fn.length
to determine how many arguments the function expects normally,
so pb.make()
will not work with functions that do not explicitly define their arguments in
their function declaration.
break(fn)
is the opposite of make(fn)
. fn
here is a function which returns a Promise.
break(fn)
will generate a new function with an extra parameter, an optional
callback(err, result)
. If no callback is provided, the generated function will behave exactly
like the original function. If a callback is provided, then the generated function will return
null
, and will pass any results that would have been returned via the Promise via the callback
instead.
Note that pb.break()
uses fn.length
to determine how many arguments the function expects normally,
so pb.break()
will not work with functions that do not explicitly define their arguments in
their function declaration.
Much like Function.prototype.apply()
,
this calls a function, but this lets you call into a function when you don't know whether the
function is expecting a callback or is going to return a Promise. fn
is the function you wish
to call, argumentCount
is the number of arguments you expect the function to take (not including
the callback). Under the hood, this will call fn
and pass in a callback as the
argumentCount + 1
th parameter. If a Promise is returned, applyFn
will assume fn
is Promise
based, otherwise applyFn
will wait for the callback to be called.
If cb
is provided, applyFn
will call into cb
with a result, otherwise applyFn
will itself
return a Promise.
This is the Function.prototype.call()
equivalent of applyFn()
.
Note that if you do not specify an argumentCount
it will default to 0. You can use this handy shortcut:
pb.callFn(function(done) {doSomething(x, y, z, done);})
.then(...)
to call into a callback based function from inside promise-based code.
Returns a new {make, break, applyFn, callFn}
object which uses the specified promiseImpl
constructor to create new Promises.
FAQs
Library to help write libraries that accept both promises and callbacks.
The npm package promise-breaker receives a total of 664,014 weekly downloads. As such, promise-breaker popularity was classified as popular.
We found that promise-breaker demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Cloudflare has launched a setup wizard allowing users to easily create and manage a security.txt file for vulnerability disclosure on their websites.
Security News
The Socket Research team breaks down a malicious npm package targeting the legitimate DOMPurify library. It uses obfuscated code to hide that it is exfiltrating browser and crypto wallet data.
Security News
ENISA’s 2024 report highlights the EU’s top cybersecurity threats, including rising DDoS attacks, ransomware, supply chain vulnerabilities, and weaponized AI.