- xml - AJAX/Jquery XML 解析
- 具有多重继承的 XML 模式
- .net - 枚举序列化 Json 与 XML
- XML 简单类型、简单内容、复杂类型、复杂内容
总结:poll()
带有回调的函数可用;我还没有发现任何使用 native promise 。我试图改编一些但没有成功。我还没有解决的问题是,当 setTimeout 调用的函数的第一个实例结束而没有任何返回时, .then()
监听它会将终止视为 false
和一个 reject()
. then()
终止并且不监听以后的返回。
问题:如何最好地帮助 .then()
函数坚持使用 resolve()
稍后返回或 reject()
?
这篇文章的其余部分是细节。阅读有帮助的内容。
可用的投票功能:我喜欢 ( https://stackoverflow.com/users/1249219/om-shankar ) Om Shankar 在 Calling a function every 60 seconds 中的回复. David Walsh 的 poll() 非常相似(位于 https://davidwalsh.name/essential-javascript-functions )。两者都使用回调并且运行良好。我发现 poll in javascript其中包括 poll()
使用 bluebird
-只有 promise 。
这是我尝试使用原生 promises 实现的尝试。
/**
* poll - checks repeatedly whether a condition exists. When the condition
* exists, returns a resolved standard promise. When it has checked
* long enough, returns a rejected standard promise.
* @param {function} fn - a caller-supplied synchronous function that
* detects a condition in the environment. Returns true if the
* condition exists; otherwise false.
* @param {number} timeout - maximum number of milliseconds
* the caller wants to check param fn();
* reject() the promise at the expiration of param timeout.
* @param {number} interval - minimum number of milliseconds between
* calls to param fn(); resolve() the promise when param fn() first
* reports true.
* @return {promise} - resolved when param fn() returns true;
* rejected if param timeout expires without param fn() returning true
*/
function poll(fn, timeout, interval) {
let endTime = Number(new Date()) + (timeout || 2000)
interval = interval || 250
return Promise.resolve *2
.then(() => { *3
(function p(fn, endTime, interval) {
if (fn()) { return Promise.resolve("Condition is satisfied.") } *4
else {
if (Number(new Date()) <= endTime) {) *5
window.setTimout(p, interval, fn, endTime, interval) *6
}
else {
return Promise.reject("Past endTime; condition not satisfied")
}
}
}()) *7
}) *8
}
预期用途:
function waitIsOver() { return (<desired condition exists>) }
poll(waitIsOver, 2000, 250) *1
我认为这是运行的方式(如果我错了请纠正我):在调用 poll()
之后在 *1,我们快速返回一个在 *2 的未决 promise ,以便 poll()
知道等待。然后,我们称该 promise 为 then()
* 3处的功能。函数p()
开始。如果fn()
(在外部已知 p()
为 waitIsOver()
)在 *4 返回真,我们很好:我们返回 resolve()
和 poll()
在 *1 得到它寻求的已解决的 promise 。
然后是不好的部分:如果fn()
在 *4 处返回 false,我们在 endTime
中在 *5(这很可能;第一次调用不太可能发生在 endTime
之后),我们使用 setTimeout()
在 *6 处要求 JS 在堆栈中做一个注释以实例化另一个 p()
在 interval
之后时间。之后,第一个实例p()
终止于 *7。在 *8,then()
知道 p()
没有返回任何东西而终止,并将条件解释为返回 false
和 reject()
;与 reject()
, promise 已经确定,永远不会改变。但是,interval
到期后,p()
的后继实例开火了。它返回的任何东西都丢失了; promise 已定,then()
在不需要的路径上发送执行后已终止。
How do I convert an existing callback API to promises?推荐使用 Promise 构造函数的方法,resolve()
打电话callback()
, 和 reject()
打电话errback
.我尝试了该技术,但遇到了与 then()
相同的问题功能在我想要之前结束。我还没有弄清楚如何制作then()
像回调函数一样耐心等待。
这提出了问题。再次:
问题:如何最好地帮助 .then()
函数坚持稍后从 resolve()
返回或 reject()
?
最佳答案
How best to help the .then() function stick around for later returns from resolve() or reject()
.then()
处理程序在底层 promise 被解决或拒绝时被调用。在此之前从未调用过它。因此,如果您想在调用 .then()
处理程序时延迟,那么您可以延迟解析或拒绝底层 promise ,直到适当的时间。
从我的评论中您可以看出,很难准确地说出您要完成的目标,因为您不只是描述您要完成的简单目标。
鉴于此,这是我对您要实现的目标的猜测。一个明确的问题可能会在几分钟内得到这样的答案。
如果你只是想重复轮询你的函数直到它返回一个真值或直到超时时间命中,你可以使用标准的 ES6 promies 来做到这一点:
function poll(fn, timeout, interval) {
return new Promise(function(resolve, reject) {
// set timeout timer
var timeoutTimer = setTimeout(function() {
clearInterval(intervalTimer);
reject("Past endTime; condition not satisfied");
}, timeout);
// set polling timer
var intervalTimer = setInterval(function() {
if (fn()) {
clearTimeout(timeoutTimer);
clearInterval(intervalTimer);
resolve("Condition is satisfied");
}
}, interval);
});
}
poll(yourFounction, 5000, 100).then(function(result) {
// succeeded here
}).catch(function(err) {
// timed out here
})
或者,对于 Bluebird promise 库,您可以使用其 .timeout()
方法来执行此操作:
function poll(fn, timeout, interval) {
return new Promise(function(resolve, reject) {
// set polling timer
var intervalTimer = setInterval(function() {
if (fn()) {
clearInterval(intervalTimer);
resolve("Condition is satisfied");
}
}, interval);
}).timeout(timeout, "Past endTime; condition not satisfied");
}
poll(yourFounction, 5000, 100).then(function(result) {
// succeeded here
}).catch(function(err) {
// timed out here
})
请注意,这两个方案都返回一个 promise,然后当 poll()
函数完成时,它们会调用 resolve 或 reject 新的 promise,然后将触发任何 .then ()
处理程序被调用。
附言我应该补充一点,这一切都假设您的 fn()
是一个同步函数,它返回一个真值或假值(这就是您的代码似乎假定的)。如果您的 fn()
实际上是一个带有回调或 promise 的异步函数,那么需要将其纳入设计中。在我们编写代码以正确使用它之前,您必须向我们展示该函数的调用约定是什么。
关于javascript - 我怎样才能让 `.then()` 保持足够长的时间以实现具有 native promise 的轮询功能?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38797983/
背景: 我最近一直在使用 JPA,我为相当大的关系数据库项目生成持久层的轻松程度给我留下了深刻的印象。 我们公司使用大量非 SQL 数据库,特别是面向列的数据库。我对可能对这些数据库使用 JPA 有一
我已经在我的 maven pom 中添加了这些构建配置,因为我希望将 Apache Solr 依赖项与 Jar 捆绑在一起。否则我得到了 SolarServerException: ClassNotF
interface ITurtle { void Fight(); void EatPizza(); } interface ILeonardo : ITurtle {
我希望可用于 Java 的对象/关系映射 (ORM) 工具之一能够满足这些要求: 使用 JPA 或 native SQL 查询获取大量行并将其作为实体对象返回。 允许在行(实体)中进行迭代,并在对当前
好像没有,因为我有实现From for 的代码, 我可以转换 A到 B与 .into() , 但同样的事情不适用于 Vec .into()一个Vec . 要么我搞砸了阻止实现派生的事情,要么这不应该发
在 C# 中,如果 A 实现 IX 并且 B 继承自 A ,是否必然遵循 B 实现 IX?如果是,是因为 LSP 吗?之间有什么区别吗: 1. Interface IX; Class A : IX;
就目前而言,这个问题不适合我们的问答形式。我们希望答案得到事实、引用资料或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
我正在阅读标准haskell库的(^)的实现代码: (^) :: (Num a, Integral b) => a -> b -> a x0 ^ y0 | y0 a -> b ->a expo x0
我将把国际象棋游戏表示为 C++ 结构。我认为,最好的选择是树结构(因为在每个深度我们都有几个可能的移动)。 这是一个好的方法吗? struct TreeElement{ SomeMoveType
我正在为用户名数据库实现字符串匹配算法。我的方法采用现有的用户名数据库和用户想要的新用户名,然后检查用户名是否已被占用。如果采用该方法,则该方法应该返回带有数据库中未采用的数字的用户名。 例子: “贾
我正在尝试实现 Breadth-first search algorithm , 为了找到两个顶点之间的最短距离。我开发了一个 Queue 对象来保存和检索对象,并且我有一个二维数组来保存两个给定顶点
我目前正在 ika 中开发我的 Python 游戏,它使用 python 2.5 我决定为 AI 使用 A* 寻路。然而,我发现它对我的需要来说太慢了(3-4 个敌人可能会落后于游戏,但我想供应 4-
我正在寻找 Kademlia 的开源实现C/C++ 中的分布式哈希表。它必须是轻量级和跨平台的(win/linux/mac)。 它必须能够将信息发布到 DHT 并检索它。 最佳答案 OpenDHT是
我在一本书中读到这一行:-“当我们要求 C++ 实现运行程序时,它会通过调用此函数来实现。” 而且我想知道“C++ 实现”是什么意思或具体是什么。帮忙!? 最佳答案 “C++ 实现”是指编译器加上链接
我正在尝试使用分支定界的 C++ 实现这个背包问题。此网站上有一个 Java 版本:Implementing branch and bound for knapsack 我试图让我的 C++ 版本打印
在很多情况下,我需要在 C# 中访问合适的哈希算法,从重写 GetHashCode 到对数据执行快速比较/查找。 我发现 FNV 哈希是一种非常简单/好/快速的哈希算法。但是,我从未见过 C# 实现的
目录 LRU缓存替换策略 核心思想 不适用场景 算法基本实现 算法优化
1. 绪论 在前面文章中提到 空间直角坐标系相互转换 ,测绘坐标转换时,一般涉及到的情况是:两个直角坐标系的小角度转换。这个就是我们经常在测绘数据处理中,WGS-84坐标系、54北京坐标系
在软件开发过程中,有时候我们需要定时地检查数据库中的数据,并在发现新增数据时触发一个动作。为了实现这个需求,我们在 .Net 7 下进行一次简单的演示. PeriodicTimer .
二分查找 二分查找算法,说白了就是在有序的数组里面给予一个存在数组里面的值key,然后将其先和数组中间的比较,如果key大于中间值,进行下一次mid后面的比较,直到找到相等的,就可以得到它的位置。
我是一名优秀的程序员,十分优秀!