首页 > 解决方案 > ember并发,如何根据条件设置timeout值

问题描述

我正在使用 ember-concurrency ,我必须每 10 秒调用一次 API 并更新我的应用程序安装阶段的设置状态。如果有错误,那么我需要超时值为 1 秒,而不是默认值为 10 秒。似乎每次超时值仅为 10 秒,即使出现错误,用户在屏幕上停留 10 秒,然后看到错误模式。谁能告诉什么是可能的解决方案?或者我做错了什么?

induceWait: task(function*() {
  while (continue) {
   // make the api call here
   //if error in any evaluating conditons, set timeout value to be 1 seconds, else 10 seconds
   this.get('store').findAll('status').then((response) => {
     if (response.flag) {
        this.set('timeout', 10000);
     } else {
        this.set('timeout', 1000);
      }
   }, (error) => {

   });
   yield timeout(this.get('timeout');
   this.get('induceWait').perform();
}

标签: ember.jsconcurrencyember-concurrency

解决方案


我认为这里问题的根源在于需要兑现承诺本身。所以,目前发生的事情是承诺“开始”,然后你timeout在你的承诺解决之前产生一个超时。

在此示例中,我使用了一个局部变量,并更改了名称以delayMs避免与timeout函数的命名冲突

induceWait: task(function*() {
  while (true) {

  let delayMs = 1000;

  const response = yield this.get('store').findAll('status');

  if (response.flag) {
    delayMs = 10000;
  } 

  yield timeout(delayMs);

  this.get('induceWait').perform();
}

推荐阅读