Skip to content

Latest commit

 

History

History
42 lines (30 loc) · 1.36 KB

README.md

File metadata and controls

42 lines (30 loc) · 1.36 KB

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.