user8675491
user8675491

Reputation:

nodejs how to use multiple await promises

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

Upvotes: 0

Views: 397

Answers (1)

Amit Wagner
Amit Wagner

Reputation: 3264

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

    });

}

Upvotes: 4

Related Questions