gpt4 book ai didi

javascript - Nodejs如何使用多个await promise

转载 作者:太空宇宙 更新时间:2023-11-04 03:03:26 25 4
gpt4 key购买 nike

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

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

最佳答案

您没有返回 promise ,这就是您收到错误的原因

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

resolve(stdout);
});

});

}

关于javascript - Nodejs如何使用多个await promise ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47795212/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com