Created
October 9, 2014 02:28
-
-
Save dulichan/3ce43ee115359e99663b to your computer and use it in GitHub Desktop.
Promises Example (uses bluebird)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | |
}); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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