Gathering detailed insights and metrics for pify
Gathering detailed insights and metrics for pify
Gathering detailed insights and metrics for pify
Gathering detailed insights and metrics for pify
npm install pify
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
1,506 Stars
69 Commits
67 Forks
15 Watching
1 Branches
15 Contributors
Updated on 18 Nov 2024
JavaScript (71.47%)
TypeScript (28.53%)
Cumulative downloads
Total Downloads
Last day
-4.6%
16,726,596
Compared to previous day
Last week
3.7%
93,124,368
Compared to previous week
Last month
26%
363,100,835
Compared to previous month
Last year
-1.4%
3,551,984,574
Compared to previous year
5
Promisify a callback-style function
1npm install pify
1import fs from 'fs'; 2import pify from 'pify'; 3 4// Promisify a single function. 5const data = await pify(fs.readFile)('package.json', 'utf8'); 6console.log(JSON.parse(data).name); 7//=> 'pify' 8 9// Promisify all methods in a module. 10const data2 = await pify(fs).readFile('package.json', 'utf8'); 11console.log(JSON.parse(data2).name); 12//=> 'pify'
Returns a Promise
wrapped version of the supplied function or module.
Type: Function | object
Callback-style function or module whose methods you want to promisify.
Type: object
Type: boolean
Default: false
By default, the promisified function will only return the second argument from the callback, which works fine for most APIs. This option can be useful for modules like request
that return multiple arguments. Turning this on will make it return an array of all arguments from the callback, excluding the error argument, instead of just the second argument. This also applies to rejections, where it returns an array of all the callback arguments, including the error.
1import request from 'request'; 2import pify from 'pify'; 3 4const pRequest = pify(request, {multiArgs: true}); 5 6const [httpResponse, body] = await pRequest('https://sindresorhus.com');
Type: Array<string | RegExp>
Methods in a module to promisify. Remaining methods will be left untouched.
Type: Array<string | RegExp>
Default: [/.+(?:Sync|Stream)$/]
Methods in a module not to promisify. Methods with names ending with 'Sync'
are excluded by default.
Type: boolean
Default: false
If the given module is a function itself, it will be promisified. Enable this option if you want to promisify only methods of the module.
1import pify from 'pify'; 2 3function fn() { 4 return true; 5} 6 7fn.method = (data, callback) => { 8 setImmediate(() => { 9 callback(null, data); 10 }); 11}; 12 13// Promisify methods but not `fn()`. 14const promiseFn = pify(fn, {excludeMain: true}); 15 16if (promiseFn()) { 17 console.log(await promiseFn.method('hi')); 18}
Type: boolean
Default: true
Whether the callback has an error as the first argument. You'll want to set this to false
if you're dealing with an API that doesn't have an error as the first argument, like fs.exists()
, some browser APIs, Chrome Extension APIs, etc.
Type: Function
Custom promise module to use instead of the native one.
util.promisify
?util.promisify
.multiArgs
).Class methods are not bound, so when they're not called on the class itself, they don't have any context. You can either promisify the whole class or use .bind()
.
1import pify from 'pify'; 2import SomeClass from './some-class.js'; 3 4const someInstance = new SomeClass(); 5 6// ❌ `someFunction` can't access its class context. 7const someFunction = pify(someClass.someFunction); 8 9// ✅ The whole class is promisified and the `someFunction` method is called on its class. 10const someClassPromisified = pify(someClass); 11someClassPromisified.someFunction(); 12 13// ✅ `someFunction` is bound to its class before being promisified. 14const someFunction = pify(someClass.someFunction.bind(someClass));
pify
choosing the last function overload when using it with TypeScript?If you're using TypeScript and your input has function overloads, then only the last overload will be chosen and promisified. This is a TypeScript limitation.
If you need to choose a different overload, consider using a type assertion:
1function overloadedFunction(input: number, callback: (error: unknown, data: number) => void): void
2function overloadedFunction(input: string, callback: (error: unknown, data: string) => void): void {
3 /* … */
4}
5
6const fn = pify(overloadedFunction as (input: number, callback: (error: unknown, data: number) => void) => void)
7// ^? (input: number) => Promise<number>
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
no dangerous workflow patterns detected
Reason
security policy file detected
Details
Reason
0 existing vulnerabilities detected
Reason
license file detected
Details
Reason
Found 10/30 approved changesets -- score normalized to 3
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
detected GitHub workflow tokens with excessive permissions
Details
Reason
dependency not pinned by hash detected -- score normalized to 0
Details
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
project is not fuzzed
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Score
Last Scanned on 2024-11-18
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More