Promises with node.js

0

Hi, I have this problem with a node.js code. The idea of the code is to copy a file inside the server, but for some reason the file information is not transmidite as a parameter to the next promise. Here you have the code I hope you can help me:

'use strict'

const fs = require('fs'),
      file = 'assets/Nombres.txt',
      newFile = 'assets/Nombres-copia.txt'

var Promesa1 = new Promise ((resolve, reject) =>{
    fs.access(file, fs.constants.F_OK, function (e){
        return (e) ? reject(new Error('El archivo no existe')) : resolve(true)
    })
}),
    Promesa2 = new Promise((resolve, reject) =>{
    console.log( 'El archivo existe.')
    fs.readFile(file, (e, data)=>{
        (e) ? reject(new Error('El archivo no se ha leido.')):resolve(data)
        return data 
    })
}),
    Promesa3 = new Promise((resolve, reject) =>{
        console.log('Data en promesa3: ', resolve)   
        console.log( 'El archivo se ha leido exitosmente.')
        fs.writeFile( newFile, resolve, (e)=>{
            return (e) ? reject(new Error('El archivo no pudo ser copiado.')) : resolve('El archivo fue copiado con exito.')
        })
})

Promesa1
    .then( (resolve, reject)=>{
        return Promesa2
    })
    .then( (resolve, reject)=>{
        return Promesa3
    })
    .then( (resolve, reject)=>{
        console.log(resolve)
    })
    .catch( (e)=>{
        console.log(e.message)
    })

Thank you.

    
asked by Antuanct 16.10.2018 в 02:39
source

1 answer

3

The problem is that you are misusing the resolve and reject of the promises within the then, and also never pass the actual data that you want to copy from promise 2 to promise 3 but you are using the direct resolve, which is native code of the promises of javascript, I recommend you to make functions that return promises, and the results of each one, to pass them to the next promise by means of parameters, of this way it would work, I hope it serves you:

'use strict'

const fs = require('fs'),
    file = 'assets/Nombres.txt',
    newFile = 'assets/Nombres-copia.txt';

const promesa1 = () => new Promise((resolve, reject) => {
    fs.access(file, fs.constants.F_OK, function (e){
        return (e) ? reject(new Error('El archivo no existe')) : resolve(true)
    })
});

const promesa2 = () => new Promise((resolve, reject) => {
    console.log( 'El archivo existe.');
    fs.readFile(file, (e, data)=>{
        (e) ? reject(new Error('El archivo no se ha leido.')): resolve(data)
        return data 
    })
})

const promesa3 = fileForCopy => new Promise((resolve, reject) => {
    console.log( 'El archivo se ha leido exitosmente.');
    fs.writeFile( newFile, fileForCopy, (e) => {
        return (e) ? reject(new Error('El archivo no pudo ser copiado.')) : resolve('El archivo fue copiado con exito.')
    })
});

promesa1()
    .then( response => {
        return promesa2();
    })
    .then( response => {
        return promesa3(response);
    })
    .then( response => {
        console.log(response)
    })
    .catch( (e)=>{
        console.log(e.message)
    });
    
answered by 16.10.2018 / 02:58
source