nodejs如何使用多个等待promises

时间:2017-12-13 14:18:23

标签: javascript node.js asynchronous async-await ecmascript-2017

我如何在我的代码中使用多承诺等待?当我想使用第二个等待第二个承诺时,它会抛出一个错误

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 个答案:

答案 0 :(得分:4)

你没有回复一个承诺,这就是你收到错误的原因

&#13;
&#13;
text = " sc\ud83d\udc7bUsername
&#13;
&#13;
&#13;