Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

how to display text value with argument from a resolved promise

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

>Solution :

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?

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading