在获得特定功能的响应后,如何重新执行该功能?

时间:2019-05-15 15:46:12

标签: 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() has returned a value
}

1 个答案:

答案 0 :(得分:0)

请等待直到Promise解决为止,如果需要,请执行任何操作。确保还处理错误:

sendMail() {
  fetch('/backend-endpoint-url`).then(response => response.json());
    .then(response => {
      // do whatever you need when the endpoint resolves
    })
    .catch(error => {
      // error handling here
    });
}