完成所有承诺后,如何使用setTimeout()?

时间:2019-06-26 04:42:30

标签: javascript

我尝试在所有诺言完成后将诺言功能设置为超时,但是它不起作用。查看我的代码。

function cashierPromise() {
  return new Promise(function(resolve, reject) {
    resolve();
  });
}

function pharPromise() {
  return new Promise(function(resolve, reject) {
    resolve();
  });
}


$(document).ready(function() {
  $('#test1').click(function() {

    Promise.all([cashierPromise(), pharPromise()])
      .then(values => {
        console.log('promise all')
        setTimeout(cashierPromise, 5000);
        setTimeout(pharPromise, 5000);
      })
      .catch(error => {
        console.log(error.message)
      });
  });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<button type="button" id="test1" class="btn">TEST1</button>

是这样还是不是? [cashierPromise,pharPromise]->全部完成->重新呼叫->所有完成->重新呼叫

3 个答案:

答案 0 :(得分:1)

我了解的是,我想您要实现的目标是。在一定间隔后再次调用此相同的函数。如果我错了请纠正我。

$(document).ready(function () {
    $('#test1').click(runPromise);
});

function runPromise() {
    Promise.all([cashierPromise(), pharPromise()])
        .then(values => {
            console.log('promise all')
            setTimeout(runPromise, 5000);
        })
        .catch(error => { 
          console.log(error.message)
        });
}

答案 1 :(得分:1)

如果我的理解正确,您希望在单击按钮后无限期地执行承诺。

实现该目标的一种方法是:

function cashierPromise() {
  return new Promise(function(resolve, reject) {
    console.log('cashierPromise')
    resolve();
  });
}

function pharPromise() {
  return new Promise(function(resolve, reject) {
    console.log('pharPromise')
    resolve();
  });
}

/* Introduce an interation function to encapsualte logic for a single
iteration/cycle of the function calls */
function iteration(number) {

  number = number || 0;
  console.log(`iteration:${number}`);

  Promise.all([cashierPromise(), pharPromise()])
    .catch(error => {
      console.log(error.message)
    })
    .then(() => {

      /* Impose 5second delay */
      return new Promise((resolve) => setTimeout(resolve, 5000))
    })
    .then(() => {

      /* Re-run iteration causing the cashierPromise() and pharPromise() 
      function to be called again */
      iteration(number + 1);
    });

}

$(document).ready(function() {


  $('#test1').click(function() {
    /* Invoke first iteration to call the two promises */
    iteration();
  });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.0.0/jquery.min.js"></script>
<button type="button" id="test1" class="btn">TEST1</button>

答案 2 :(得分:0)

如何使用异步/等待?

function cashierPromise() {
  return new Promise(function(resolve, reject) {
    resolve();
  });
}

function pharPromise() {
  return new Promise(function(resolve, reject) {
    resolve();
  });
}

function wait(ms) {
  return new Promise(resolve => setTimeout(resolve, ms));
}

$(document).ready(function() {
  $('#test1').click(async function() {

    try {
      for(;;) {
        const value = await Promise.all([cashierPromise(), pharPromise()]);
        console.log('promise all');
        await wait(5000);
      }
    } catch(error) {
      console.log(error.message)
    }
    
  });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<button type="button" id="test1" class="btn">TEST1</button>