Created
January 14, 2015 03:48
-
-
Save tuliofaria/39955d5fb489114f2ee6 to your computer and use it in GitHub Desktop.
Promises com Q
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
// 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); | |
}); |
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 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