as shown in the below posted code, i am returning a promise form a function.i would like to display a text message with argument when the promise is resolved. in other words, for the following line of code:
resolve("successfully saved the data to file:",{fileName+newExt})
when the promise is resolved i receive the aforementioned text but without the value of `fileName+newExt'
i tried the following:
resolve("successfully saved the data to file:",{d:fileName+newExt})
resolve("successfully saved the data to file:",fileName+newExt)
but the always the text gets displayed without the value of fileName+ext
update
as shown in code2 section posted below, i know how to print the message when the promise is resolved. but the text message in the resolve()
gets displayed without the value of fileName+ext
code
export function writeToFile(fileName,contents,ext='.tiff') {
let newExt = ''
return new Promise((resolve,reject)=> {
if (typeof (fileName) !== "string") {
reject(new Error("fileName is not a string.Did you pass string-object using new String(..)"))
}
if (contents == undefined) {
reject(new Error("contents to be written to the file is undefined"))
}
if (typeof (ext) !== "string") {
reject(new Error("extension is not a string.Did you pass string-object using new String(..)"))
}
if (ext.charAt(0) === '.') {
newExt = ext
} else {
newExt = '.' + ext
}
if (!fs.existsSync(envVars.DEBUG_OUTPUT_PATH_FOR_TIFFS)) {
fs.mkdirSync(path, {recursive:true})
}
fs.writeFile(envVars.DEBUG_OUTPUT_PATH_FOR_TIFFS + fileName + ext, contents,{flag:'w'},(error)=> {
if (error) {
reject(new Error("error occured while writing data to file.error:",error," file:",fileName+newExt))
throw error
}
resolve("successfully saved the data to file:",{d:fileName+newExt})
});
})
}
code2
response.on('close', async()=>{
const bufferedData = Buffer.concat(data)
writeToFile("test", bufferedData,'.tiff')
.then(statusMsg => {
console.log("write-to-file status message:", statusMsg)
fromFile(envVars.DEBUG_OUTPUT_PATH_FOR_TIFFS + "test" + envVars.CONST_TIFF_EXT)
.then(geoTIFF=> {
geoTIFF.getImage()
.then(geoTIFFImage=> {
console.log("geoTIFFImage:",geoTIFFImage.getBoundingBox())
})
})
})