skip to Main Content

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())
        })
    })
})

2

Answers


  1. You should use JavaScript template literals

    resolve(`successfully saved the data to file:${fileName}${newExt}`)
    

    If for some reason you do not have access to template literals, use the + sign to concatenate:

    resolve("successfully saved the data to file:" + fileName + newExt)
    

    You can then call your function as follows:

    writeToFile(fileName, contents, '.tiff').then(console.log).catch(console.error);
    

    You also do not need to reject and to throw. Either one will reject the promise, and you can catch the error as shown on the example above with .catch((error) => console.error(error))

    Login or Signup to reply.
  2. You cannot resolve multiple values from the promise, which is why you don’t get file string, or object.

    So, you should change the result returned from the function.

    For example, always send an object with message/result/error property

    resolve({message:'msg'});
    

    the same goes for error:

    reject({message:error});
    

    or maybe an array, or string, but only a single argument.

    see:

    How do you properly return multiple values from a Promise?,

    Can promises have multiple arguments to onFulfilled?

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search