在承诺中超时函数的最佳一般做法是什么 [英] What is the best general practice to timeout a function in promise

查看:20
本文介绍了在承诺中超时函数的最佳一般做法是什么的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用超时承诺函数调用

我看到许多资源提供了使用 Promise.race 在给定时间段内使函数调用超时的类似示例.这是 Promise.race 如何在实践中使用的一个很好的例子.下面是一些示例代码:

I have seen many resources provide similar examples of using Promise.race to timeout a function call within a given period of time. This is a very good example of how Promise.race can be used in practice. Here's some sample code:

function doWithinInterval(func, timeout) {
    var promiseTimeout = new Promise(function (fulfill, reject) {
       // Rejects as soon as the timeout kicks in
       setTimeout(reject, timeout);
    });
    var promiseFunc = new Promise(function (fulfill, reject) {
        var result = func(); // Function that may take long to finish
        // Fulfills when the given function finishes
        fulfill(result);
    });

    return Promise.race([promiseTimeout, promiseFunc]);
}

上述使用 Promise.race 的简单方法会在 func 完成之前超时一开始就拒绝承诺.否则,一旦 func 函数在超时间隔之前完成,项目就会完成.

The simple approach above using Promise.race rejects the promise as soon as the timeout kicks in before func has completed. Otherwise, the project is fulfilled once the func function finishes before the timeout interval.

这听起来不错且易于使用.

This sounds good and easy to use.

但是,这是在 Promise 中使用超时的最佳做法吗?

当然,如果我们想为使用 Promise 的函数调用设置超时,则可以采用上述方法.这些操作似乎仍然是一个很好的承诺.但是,这被认为是在 Promise 中使用超时的好习惯吗?如果不是,使用它的缺点是什么?

Surely, the approach above can be employed if we want to set a timeout against a function call using Promises. The operations still appear to be a good promise. However, is this considered a good practice of using timeout in a Promise? If not, what is the disadvantage of using this?

我一直在寻找替代方法,但找不到原生的 Promise 方式来做到这一点.

相反,一些外部 Promise 库提供了 timeout 功能,如下所示:

Instead, some external Promise libraries offer timeout functionality as follows:

WinJS 提供 .timeout() 以及

WinJS supplies .timeout() as well

Q 还带有 .timeout().

然而,Promise.timeout() 似乎不是标准 ECMAScript 6 API 的一部分(如果我错了,请纠正我).是否有任何推荐的方法可以使用 ES6 Promises 在本机处理超时?

However, Promise.timeout() doesn't appear to be part of the standard ECMAScript 6 API (please correct me if I'm wrong). Is there any recommended way to handle timeouts natively with ES6 Promises?

推荐答案

这取决于您所说的超时是什么意思.

It depends on what you mean by timeout.

如果您希望该函数停止,则不会.

If you expect the function to stop, then no.

如果你只是不想等待它,那么是的(在 ES6 中快速启动):

If you just want to stop waiting for it, then yes (quick to whip up in ES6):

var wait = ms => new Promise(resolve => setTimeout(resolve, ms));
var timeout = (p, ms) => Promise.race([p, wait(ms).then(() => {
    throw new Error("Timeout after " + ms + " ms");
})]);

var wait = ms => new Promise(resolve => setTimeout(resolve, ms));
var timeout = (p, ms) => Promise.race([p, wait(ms).then(() => {
  throw new Error("Timeout after " + ms + " ms");
})]);

// Example:

var log = msg => div.innerHTML += "<p>" + msg + "</p>";
var failed = e => log(e.toString() + ", line " + e.lineNumber);

log("Waiting 5 seconds...");
timeout(wait(5000), 2000)
.then(() => log("...Done."))
.catch(failed);

<div id="div"></div>

如果你想取消操作(让它停止),那么希望该操作带有一个 API 来取消它,你应该使用它,因为 ES6 承诺不是控制面.

If you want to cancel the operation (make it stop), then hopefully that operation comes with an API to cancel it, and you should use that, since an ES6 promise is not a control surface.

可取消的承诺在 ES6 中是一个有争议的话题,但提到的一些库确实提供了这个概念.

Cancelable promises is a controversial topic in ES6, but some of the libraries mentioned do offer the concept.

这篇关于在承诺中超时函数的最佳一般做法是什么的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆