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
    }
}
Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143

1 Answers1

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

    });

}
Amit Wagner
  • 3,134
  • 3
  • 19
  • 35
  • 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? –  Dec 13 '17 at 14:34
  • 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 – Amit Wagner Dec 13 '17 at 14:58