Skip to content

💌 Convert asynchronous function calls with callbacks to es6 promises

Notifications You must be signed in to change notification settings

noseglid/voucher

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 

Repository files navigation

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.

About

💌 Convert asynchronous function calls with callbacks to es6 promises

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published