Skip to content

Instantly share code, notes, and snippets.

@dulichan
Created October 9, 2014 02:28
Show Gist options
  • Save dulichan/3ce43ee115359e99663b to your computer and use it in GitHub Desktop.
Save dulichan/3ce43ee115359e99663b to your computer and use it in GitHub Desktop.
Promises Example (uses bluebird)
var Promise = require('bluebird')
var fs = require('fs');
Promise.promisifyAll(fs);
fs.readFile("bb", function(err, data) {
if (err) return next(err);
fs.writeFile("bba", data, function() {
console.log("File written");
});
});
var Promise = require('bluebird')
var fs = require('fs');
Promise.promisifyAll(fs);
fs.readFileAsync("bb").then(function(data) {
return fs.writeFileAsync("bba", data);
}).then(function() {
return "bba";
}).then(function(name) {
console.log("File " + name + " written");
}).
catch (function(e) {
console.error("unable to read file")
});
var Promise = require('bluebird')
var fs = require('fs');
Promise.promisifyAll(fs);
var promise = fs.readFileAsync("bb").then(function(data) {
return fs.writeFileAsync("bba", data);
}).then(function() {
return "bba";
}).
catch (function(e) {
console.error("unable to read file")
});
// in another layer
promise.then(function(name) {
console.log("File " + name + " written");
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment