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.
The thenify npm package is designed to convert Node.js callback-style functions to functions that return a promise. This is particularly useful when working with older Node.js APIs or third-party libraries that do not natively support promises, allowing developers to write cleaner, more modern asynchronous code using async/await or .then() syntax.
Converting a callback-style function to a promise-returning function
This feature allows you to wrap a traditional Node.js callback-style function, such as `fs.readFile`, in a way that it returns a promise. This enables the use of `.then()` and `.catch()` for cleaner asynchronous control flow.
const thenify = require('thenify');
const fs = require('fs');
const readFile = thenify(fs.readFile);
readFile('example.txt', 'utf8').then(contents => {
console.log(contents);
}).catch(err => {
console.error(err);
});
Introduced in Node.js 8, `util.promisify` is a built-in function that converts a callback-based function into a promise-based one. It serves a similar purpose to thenify but is built into the Node.js runtime. Unlike thenify, it does not need to be installed as an external package, offering a more integrated solution for modern Node.js versions.
Bluebird is a comprehensive promise library that offers a wide range of features for working with promises, including but not limited to converting callback-style functions into promises. It provides a method called `.promisify()` which is similar to thenify's functionality. Bluebird promises are known for their performance and additional utility methods not found in native promises, making it a more feature-rich, albeit heavier, alternative.
Pify is another npm package that converts callback-based functions into promises. It offers a simple and lightweight approach similar to thenify but with additional options for customizing the behavior of the promisified function, such as the ability to handle multiple callback arguments or to exclude certain functions from promisification. Pify provides a balance between simplicity and configurability.
Promisify a callback-based function.
bluebird
Array
An added benefit is that throw
n errors in that async function will be caught by the promise!
var thenify = require('thenify');
var somethingAsync = thenify(function somethingAsync(a, b, c, callback) {
callback(null, a, b, c);
});
var thenify = require('thenify').withCallback;
var somethingAsync = thenify(function somethingAsync(a, b, c, callback) {
callback(null, a, b, c);
});
// somethingAsync(a, b, c).then(onFulfilled).catch(onRejected);
// somethingAsync(a, b, c, function () {});
Promisifies a function.
FAQs
Promisify a callback-based function
The npm package thenify receives a total of 11,585,614 weekly downloads. As such, thenify popularity was classified as popular.
We found that thenify 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.
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.