1.3.1 • Published 7 years ago
js-promisify v1.3.1
JS Promisify
Introduction
JS Promisify is a npm minimal and well-tested async module for NodeJS, that converts NodeJS async style functions to native JS promises (if they're supported by the underlying JavaScript engine, like in Node >= 4.0.0).
For example, it can be used to convert the NodeJS native file system library (fs), to easily perform file operations with promises.
Install
npm install js-promisify
(add "--save" if you want the module to be automatically added to your project's "package.json" dependencies)
Usage
promisify(fun, args, [ctx])funcan be any function with a callback argument that follows the Node JS async function pattern signature (i.e. the callback is the last argument and has a signaturefunction(err, data)).argsis an array containing all the arguments that need to be passed tofun, excluding the callback.ctx(optional) is what the variablethisshould be bound to whenfunis called with the arguments provided through theargsarray.
Example
const promisify = require('js-promisify');
const fs = require('fs');
promisify(fs.readFile, ['path/to/myfile.txt', {encoding: 'utf8'}])
.then(function (data) {
console.log(data);
})
.catch(function (err) {
console.log(err);
})
// Same as above, using async / await
(async () => {
console.log(await promisify(fs.readFile, ['path/to/myfile.txt', {encoding: 'utf8'}]))
})()MIT License