gpt4 book ai didi

javascript - 如何在 google 和 API 的操作中使用 async 和 wait

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

我正在使用 API 在 google 上执行操作,并且我正在使用请求 promise 来构建它。但是在调用 api 时,我计算出的平均时间是 0.5 秒,所以我试图等待 0.5 秒,但在部署此代码时出现错误

 .then( async(parseBody)=>{

for(var i=0;i<parseBody.passengers.length;i++){
data.push(parseBody.passengers[i].current_status);

}
console.log(data);
// sleep.sleep(0.5);
await sleep(2000)
conv.ask(`Ticket's current status is ${data}`);

错误是32:33错误解析错误:意外的标记=>
请帮助我找到更好的解决方案,我是第一次使用 API

最佳答案

使用 Promise 可以连接更多 .then()

所以,你可以这样做:

.then( (data) => {
//do stuff with data
return data;
})
.then( (data) => {
//Here data is what you returned in then() before
//do stuff with data
});

顺便说一句,您收到的错误是因为您的代码中有语法错误,它应该是:

.then( async(parseBody)=>{
for(var i=0;i<parseBody.passengers.length;i++){
data.push(parseBody.passengers[i].current_status);

}
console.log(data);
// sleep.sleep(0.5);
await sleep(2000)
conv.ask(`Ticket's current status is ${data}`);
});

关于javascript - 如何在 google 和 API 的操作中使用 async 和 wait,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52535009/

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