A clean way to promisify callback functions [Node.js, nodejitsu, npmawesome, promises and bluebird]

Forked from Hello World for Node.js.

27 Runs 157 Views 8 Copies

This example shows the usage of Songbird module which wraps Bluebird and give a very clean `promise` property on all callback style functions.

Saved

Saved

alexgorbatchev 15

alexgorbatchev
published 3 months ago

var Promise = require('songbird');
var fs = require('fs');

// callback API
fs.readFile('./package.json', 'utf8', function(err, pkg) {
  console.log('callbacks', pkg);
});

// songbird API
fs.promise.readFile('./package.json', 'utf8').then(function(pkg) {
  console.log('promise', pkg);
});

Promise
  .join(
    fs.promise.readFile('./package.json', 'utf8'),
    fs.promise.readFile('./songbird.js', 'utf8')
  )
  .spread(function(pkg, songbird) {
    console.log('package.json length is', pkg.length);
    console.log('songbird.js length is', songbird.length);
  });

Terminal

Your session has timed out.

Dismiss (the page may not function properly).