Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const fs = require('fs');
- const util = require('util');
- const fsPromisified = util.promisify(fs.readFile);
- function postProcesar(texto) {
- const p = new Promise(function(resolve, reject) {
- setTimeout(function() {
- if (Math.random() > 0.5) {
- reject('Error random.');
- } else {
- const resultado = texto.toUpperCase();
- resolve(resultado);
- }
- }, 1000);
- });
- return p;
- }
- fsPromisified('50-mensaje.txt', 'utf8')
- .then(function(texto) {
- console.log(`Texto del fichero: ${texto}.`);
- const promisePostProcesar = postProcesar(texto);
- return promisePostProcesar;
- }).then(function(textoProcesado) {
- console.log(`El texto procesado es ${textoProcesado}.`);
- }).catch(function(err) {
- console.log(`Error de lectura: ${err}.`);
- });
Advertisement
Add Comment
Please, Sign In to add comment