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

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

问题描述

使用超时宣传函数调用

我看过许多资源提供了使用 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中使用超时的最佳做法吗?

当然,如果我们想要使用Promises为函数调用设置超时,可以使用上面的方法。这些行动似乎仍然是一个很好的承诺。但是,这被认为是在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方法来做到这一点。

I've look for alternative approaches, but couldn't find a native Promise way to do this.

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

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

WinJS 耗材< a href =https://msdn.microsoft.com/en-us/library/windows/apps/br229729.aspx\"rel =noreferrer> .timeout() 以及

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?

推荐答案

这取决于你的意思 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.

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

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