Research
Security News
Malicious npm Package Targets Solana Developers and Hijacks Funds
A malicious npm package targets Solana developers, rerouting funds in 2% of transactions to a hardcoded address.
thenify-all
Advanced tools
The thenify-all npm package is used to promisify entire modules or specific sets of functions. This means it can convert callback-based functions or methods to return promises instead, allowing for more modern, cleaner asynchronous code using promises or async/await syntax.
Promisifying an entire module
This code sample demonstrates how to promisify all methods of the Node.js 'fs' module. After promisifying, methods like 'readFile' return a promise that can be used with '.then()' and '.catch()' for handling asynchronous operations.
const thenifyAll = require('thenify-all');
const fs = thenifyAll(require('fs'));
fs.readFile('file.txt', 'utf8').then(contents => {
console.log(contents);
}).catch(error => {
console.log(error);
});
Promisifying selected functions
This code sample shows how to promisify only selected functions from the 'fs' module. In this case, only 'readFile' and 'writeFile' are promisified, and they can be used with promise syntax.
const thenifyAll = require('thenify-all');
const fs = require('fs');
const promisifiedFs = thenifyAll(fs, {}, ['readFile', 'writeFile']);
promisifiedFs.readFile('file.txt', 'utf8').then(contents => {
console.log(contents);
}).catch(error => {
console.log(error);
});
Bluebird is a full-featured promise library with a focus on innovative features and performance. It includes utilities for converting callback-based functions to promises, similar to thenify-all, but also offers a wide range of additional features like cancellation, progress tracking, and more sophisticated error handling.
Util.promisify is a function included in the Node.js standard library that converts a callback-based function into a promise-based one. It is similar to thenify-all in its basic functionality but is built into Node.js and does not require an additional package. It does not, however, promisify entire modules at once.
Pify is a lightweight promise utility that works similarly to thenify-all by converting callback-based functions to promises. It offers a simple API and supports promisifying object methods, but unlike thenify-all, it also allows for options to customize the promisification process, such as setting multiArgs to handle functions that have multiple success parameters.
Promisifies all the selected functions in an object.
var thenifyAll = require('thenify-all');
var fs = thenifyAll(require('fs'), {}, [
'readFile',
'writeFile',
]);
fs.readFile(__filename).then(function (buffer) {
console.log(buffer.toString());
});
Promisifies all the selected functions in an object.
source
- the source object for the async functionsobj
- the destination to set all the promisified methodsmethods
- an array of method names of source
Promisifies all the selected functions in an object and backward compatible with callback.
source
- the source object for the async functionsobj
- the destination to set all the promisified methodsmethods
- an array of method names of source
Exports thenify this package uses.
FAQs
Promisifies all the selected functions in an object
We found that thenify-all demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 3 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.
Research
Security News
A malicious npm package targets Solana developers, rerouting funds in 2% of transactions to a hardcoded address.
Security News
Research
Socket researchers have discovered malicious npm packages targeting crypto developers, stealing credentials and wallet data using spyware delivered through typosquats of popular cryptographic libraries.
Security News
Socket's package search now displays weekly downloads for npm packages, helping developers quickly assess popularity and make more informed decisions.