0

how can i use multi promise await in my codes ? when i wanna use second await for second promise it throw an error

function ReadJSONFile() {
    return new Promise((resolve, reject) => {
        fs.readFile('import.json', 'utf-8', (err, data) => { 
            if (err) reject(err);
            resolve(JSON.parse(data));
         });
    });
}

const Get_Image = async (Path) => {

    Child_Process = exec('node get_image.js "'+Path+'",(err,stdout,stderr) =>
         return new Promise((resolve,reject) => {
            resolve(stdout);
         });



}


const Catch = async () => {
    let get_json_file = await ReadJSONFile(); // this works perefectly

    for(var i=0;i< Object.keys(get_json_file);i++) {
        console.log(await Get_Image(get_json_file[i].image_path); //but this throw error
    }
}

1 Answer 1

4

you didn`t return a promise that is why you got an error

const Get_Image = async (Path) => {
   return new Promise((resolve,reject) => {
    Child_Process = exec('node get_image.js "'+Path+'",(err,stdout,stderr) =>
      
            resolve(stdout);
         });

    });

}

Sign up to request clarification or add additional context in comments.

2 Comments

but if you see my code , i returned new promise with a value , whats difference ? you returned all child process i returned just value , why this is not correct?
you didnt return a promise you return undefined as you return nothing. exec function is async it takes a callback that mean that your return value from this function is long gone from your sync code . await keywork works with promise

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.