Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/384.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在得到某个函数的响应后,如何重新执行该函数?_Javascript_Node.js_Function_Delay_Nodemailer - Fatal编程技术网

Javascript 在得到某个函数的响应后,如何重新执行该函数?

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循环的迭代,直到从函数调用得到响应为止

我使用了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() 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