如何在 Promise.race() 中设置超时?
How to set Timeout in Promise.race()?
我想在我的 angular 组件中使用 promise.race 函数,但遇到超时问题。即使我已将超时定义为 promise.race 内的 1 秒,响应仍会等待 15 秒才能执行?这是我的代码:
let statusFind = this.getStatus(); //This takes 15 seconds
let statusTimeout = new Promise((resolve, reject) => { // This resolved in 1 second
let wait = setTimeout(() => {
clearTimeout(wait);
let status = {connection : 0 , online : -3, isPrintable : false };
resolve (status);
}, 1000)
});
Promise.race([ statusFind, statusTimeout]).then((statusResponse: any) => {
// Here I am getting my status response back in 15 second even if my timeout promise gets resolved first.
//Though I am getting the value here as my timeout value but why it is waiting for my 15 second first promise to finish?
});
请帮忙。
AngularJS 通过提供自己的事件处理循环来修改正常的 JavaScript 流程。这将 JavaScript 分为经典和 AngularJS 执行上下文。只有在 AngularJS 执行上下文中应用的操作才会受益于 AngularJS data-binding、异常处理、属性 监视等
ES6 承诺未与 AngularJS 框架及其摘要循环集成。
改为使用 AngularJS $q
服务创建的承诺:
̶P̶r̶o̶m̶i̶s̶e̶.̶r̶a̶c̶e̶(̶[̶ ̶s̶t̶a̶t̶u̶s̶F̶i̶n̶d̶,̶ ̶s̶t̶a̶t̶u̶s̶T̶i̶m̶e̶o̶u̶t̶]̶)̶.̶t̶h̶e̶n̶(̶(̶s̶t̶a̶t̶u̶s̶R̶e̶s̶p̶o̶n̶s̶e̶:̶ ̶a̶n̶y̶)̶ ̶=̶>̶ ̶{̶
$q.race([ statusFind, statusTimeout]).then((statusResponse: any) => {
// Here I am getting my status response back in 15 second even if my timeout promise gets resolved first.
//Though I am getting the value here as my timeout value but why it is waiting for my 15 second first promise to finish?
});
有关详细信息,请参阅
我想在我的 angular 组件中使用 promise.race 函数,但遇到超时问题。即使我已将超时定义为 promise.race 内的 1 秒,响应仍会等待 15 秒才能执行?这是我的代码:
let statusFind = this.getStatus(); //This takes 15 seconds
let statusTimeout = new Promise((resolve, reject) => { // This resolved in 1 second
let wait = setTimeout(() => {
clearTimeout(wait);
let status = {connection : 0 , online : -3, isPrintable : false };
resolve (status);
}, 1000)
});
Promise.race([ statusFind, statusTimeout]).then((statusResponse: any) => {
// Here I am getting my status response back in 15 second even if my timeout promise gets resolved first.
//Though I am getting the value here as my timeout value but why it is waiting for my 15 second first promise to finish?
});
请帮忙。
AngularJS 通过提供自己的事件处理循环来修改正常的 JavaScript 流程。这将 JavaScript 分为经典和 AngularJS 执行上下文。只有在 AngularJS 执行上下文中应用的操作才会受益于 AngularJS data-binding、异常处理、属性 监视等
ES6 承诺未与 AngularJS 框架及其摘要循环集成。
改为使用 AngularJS $q
服务创建的承诺:
̶P̶r̶o̶m̶i̶s̶e̶.̶r̶a̶c̶e̶(̶[̶ ̶s̶t̶a̶t̶u̶s̶F̶i̶n̶d̶,̶ ̶s̶t̶a̶t̶u̶s̶T̶i̶m̶e̶o̶u̶t̶]̶)̶.̶t̶h̶e̶n̶(̶(̶s̶t̶a̶t̶u̶s̶R̶e̶s̶p̶o̶n̶s̶e̶:̶ ̶a̶n̶y̶)̶ ̶=̶>̶ ̶{̶
$q.race([ statusFind, statusTimeout]).then((statusResponse: any) => {
// Here I am getting my status response back in 15 second even if my timeout promise gets resolved first.
//Though I am getting the value here as my timeout value but why it is waiting for my 15 second first promise to finish?
});
有关详细信息,请参阅