Javascript 在得到某个函数的响应后,如何重新执行该函数?
好吧这就是我想做的, 我希望保持for循环的迭代,直到从函数调用得到响应为止 我使用了setTimeout函数,但它就是不起作用Javascript 在得到某个函数的响应后,如何重新执行该函数?,javascript,node.js,function,delay,nodemailer,Javascript,Node.js,Function,Delay,Nodemailer,好吧这就是我想做的, 我希望保持for循环的迭代,直到从函数调用得到响应为止 我使用了setTimeout函数,但它就是不起作用 for(var i=0;i<5;i++) { sendMail(); <<--- Hold the iteration until this function completes and returns a value //Some other code which should be executed after sendMail
for(var i=0;i<5;i++)
{
sendMail(); <<--- Hold the iteration until this function completes and returns a value
//Some other code which should be executed after sendMail() has returned a value
}
等待承诺解决,如果是,做你需要的任何事情。同时确保处理错误:
sendMail() {
fetch('/backend-endpoint-url`).then(response => response.json());
.then(response => {
// do whatever you need when the endpoint resolves
})
.catch(error => {
// error handling here
});
}
我想sendMail是异步的。。。?确保您的sendMail正在返回承诺。然后将其更改为等待sendMail;。您向我们展示的代码需要包装在异步函数中,而不是普通函数中。有关详细信息:。您必须等待sendMail或将函数调用推送到数组,然后再执行For循环调用Promise.allarr