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.