ember.js - 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();
}
解决方案
我认为这里问题的根源在于需要兑现承诺本身。所以,目前发生的事情是承诺“开始”,然后你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();
}
推荐阅读
- node.js - mongoose 中的模块导出
- javascript - Cannot read property 'find' of undefined in Angular
- php - Ajax php数据库选择不起作用
- java - 使用 Java 中的身份验证标头调用 URL
- html - 是否可以使用主题功能覆盖插件功能
- ethereum - 将三个整数编码为单个整数
- python-3.x - 从 Python 3 中的文本中一起删除两个单词(短语)
- node.js - 使用接口映射来自 http 请求的嵌套数组响应
- javascript - 将身份发送到控制器发布操作
- elasticsearch - 仅当数组中存在所有值时,弹性搜索才选择文档