Security News
pnpm 10.0.0 Blocks Lifecycle Scripts by Default
pnpm 10 blocks lifecycle scripts by default to improve security, addressing supply chain attack risks but sparking debate over compatibility and workflow changes.
promise-callbacks
Advanced tools
This package helps you work with a codebase that uses promises instead of callbacks in
most-but-not-all places. It differs from most other callback-to-promise libraries out there by
preferring a deferred variant of a Promise
with a Node callback-compliant defer
method.
As such, its difference is most significantly that it focuses on interoperating with callbacks at
the call site.
It also uses native promises not Bluebird etc.
This is because it's 2017, and this package assumes that you'll convert all your own code to use
native promises (especially now that recent versions of Chrome and Node 7.6.0 natively support
async
/await
) and the API calls left over will be 3rd-party libraries that you really don't want
to patch, due to not having access to library classes and/or the general hackiness of
monkey-patching (just try to
trace this).
Hopefully these 3rd-party libraries will get their
acts
together in the relatively
near future. In the meantime, there's promise-callbacks
to keep it simple.
yarn add promise-callbacks
or
npm install promise-callbacks --save
The minimum requirement is a native Promise
implementation, though you'll get the most out of this
if you're using Chrome minus 2 or Node 7.6.0 or higher for async
/await
.
const { deferred } = require('promise-callbacks');
function respondWithDelay(done) {
setTimeout(() => done(null, 'hi'), 2000);
}
async function foo() {
const promise = deferred();
respondWithDelay(promise.defer());
console.log(await promise);
}
What happened there is that promise.deferred()
took the result of respondWithDelay
, as a
callback, and resolved
/rejected
the associated Promise
.
To support callbacks that provide several values, you have two options: as an array - where you can destructure into your own variables, or as an object, with a similar outcome.
const { deferred } = require('promise-callbacks');
function manyValues(done) {
setTimeout(() => {
done(null, 'several', 'values', 'here');
}, 2000);
}
async function asArray() {
const promise = deferred({variadic: true});
respondWithDelay(promise.defer());
const [first, second, third] = await promise;
console.log(`${first} ${second} ${third}`);
}
async function asObject() {
const promise = deferred({variadic: ['first', 'second', 'third']});
respondWithDelay(promise.defer());
const {first, second, third} = await promise;
console.log(`${first} ${second} ${third}`);
}
The promisify
function is based off of Node 8's util.promisify
. It works on versions of Node
prior to 8, and has special support for callbacks with multiple values, and has utilities to create
a copy of an object with promise-returning methods.
const { promisify } = require('promise-callbacks');
function respondWithDelay(done) {
setTimeout(() => done(null, 'hi'), 2000);
}
const respondWithDelayPromised = promisify(respondWithDelay);
async function foo() {
console.log(await respondWithDelayPromised());
}
const { promisify } = require('promise-callbacks');
const fs = require('fs');
// Note that readFile and writeFile are internally bound to fs, so they can interact with the
// original context object as they expect.
const { readFile, writeFile } = promisify.methods(fs, ['readFile', 'writeFile']);
readFile('input')
.then((content) => writeFile('output', content))
.catch((err) => console.error('err', err));
// If you know all the methods of the object are asynchronous, use promisify.all:
const api = {
respondWithDelay
};
const promiseAPI = promisify.all(api);
async function foo() {
console.log(await promiseAPI.respondWithDelay());
}
const { asCallback } = require('promise-callbacks');
asCallback(Promise.resolve(true), (err, res) => {
console.log(res); // true
});
Straightforward. Or, if you don't mind just a little bit of monkey-patching:
const { patchPromise } = require('promise-callbacks');
// Call this once, when your application starts up,
// to add `asCallback` to `Promise.prototype`.
patchPromise();
// Thereafter:
Promise.resolve(true).asCallback((err, res) => {
console.log(res); // true
});
example/app.js
demonstrate these APIs' use in the context of a web server. Do yarn run example
to start it.
asCallback
is inspired by Bluebird.
FAQs
Utilities to help convert a callback-using codebase to promises.
The npm package promise-callbacks receives a total of 18,036 weekly downloads. As such, promise-callbacks popularity was classified as popular.
We found that promise-callbacks demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 25 open source maintainers 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
pnpm 10 blocks lifecycle scripts by default to improve security, addressing supply chain attack risks but sparking debate over compatibility and workflow changes.
Product
Socket now supports uv.lock files to ensure consistent, secure dependency resolution for Python projects and enhance supply chain security.
Research
Security News
Socket researchers have discovered multiple malicious npm packages targeting Solana private keys, abusing Gmail to exfiltrate the data and drain Solana wallets.