🚀 DAY 5 OF LAUNCH WEEK: Introducing Socket Firewall Enterprise.Learn more →
Socket
Book a DemoInstallSign in
Socket

voucher

Package Overview
Dependencies
Maintainers
1
Versions
3
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

voucher

Convert asynchronous function calls with callbacks to es6 promises

latest
Source
npmnpm
Version
0.1.2
Version published
Maintainers
1
Created
Source

Voucher

Promises is coming in the new ES6 spec. This excellent article should convince you to use it.

A lot of libraries today still takes callback arguments rather than returning promises. Voucher will wrap the calls and give you a promise to work with. Easy.

Install

npm install voucher

Examples

NodeJS

var crypto = require('crypto');
var voucher = require('voucher');

voucher(crypto.pseudoRandomBytes, 12).then(function(randomBytesBuffer) {
  console.log(randomBytesBuffer.toString('hex'));
});
var fs = require('fs');
var voucher = require('voucher');

voucher(fs.readFile, '/etc/passwd', { encoding: 'utf8' }).then(function(content) {
  console.log(content); // Logs the contents of '/etc/passwd'
}).catch(function(error) {
  console.log('Something went wrong when reading file:', error);
});

Requirements

  • The callback argument of the function specified must be the last argument.
  • The callback must be invoked with error as its first argument.

All NodeJS native functions works well. For instance, fs.readFile takes a filename parameter, then an optional options object and then the callback function. The callback function receives two parameters, first err which indicates if anything went wrong, then the result of the readFile operation.

Keywords

promise

FAQs

Package last updated on 31 Aug 2014

Did you know?

Socket

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.

Install

Related posts