Skip to content

Instantly share code, notes, and snippets.

@tuliofaria
Created January 14, 2015 03:48
Show Gist options
  • Save tuliofaria/39955d5fb489114f2ee6 to your computer and use it in GitHub Desktop.
Save tuliofaria/39955d5fb489114f2ee6 to your computer and use it in GitHub Desktop.
Promises com Q
// promises
var q = require("q")
var fs = require('fs');
var filename = "dados.txt";
function lerDados(filename){
var defer = q.defer();
fs.readFile(filename, 'utf8', function(err, data) {
//defer.resolve(data);
defer.reject(data);
});
return defer.promise;
}
function gravarBD(data){
var defer = q.defer();
fs.writeFile("dados1.txt", data+" 1", function(err) {
defer.resolve(data);
});
return defer.promise;
}
function gerarLog(data){
var defer = q.defer();
fs.writeFile("dados2.txt", data+" 2", function(err) {
defer.resolve(data);
});
return defer.promise;
}
lerDados(filename)
.then(gravarBD)
.then(gerarLog)
.then(function(data){
console.log("Processo finalizado");
}, function(err){
console.log("erro "+err);
});
var fs = require('fs');
var filename = "dados.txt";
fs.readFile(filename, 'utf8', function(err, data) {
fs.writeFile("dados1.txt", data+" 1", function(err) {
fs.writeFile("dados2.txt", data+" 2", function(err) {
console.log("Processo terminado");
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment