我们正在使用 ECMAScript 6 Promise
。
我们需要向最终用户实现进度通知(这是纯粹的用户体验要求)。我知道其他 Promise 框架(例如 Q Promise 库)允许这样做。
我们如何最优雅采用某种进度指示?
或者我们应该迁移到不同的框架? (我不知道如何估计后者的努力)
最佳答案
ES2015 promise 永远不会有进展。 promise 代表单一的最终值(value)。如果您想要多个值,您可以查看可观察量 - 或将进度放在 promise 返回函数上。
将进度放在 promise 返回函数上非常容易。基本上,您将回调作为函数的参数,并在发生进度通知时调用它。
以下是改编 self 们的指南的一些文字:bluebird :
Progression 与使用 Promise Progress 处理程序的 API 存在可组合性和链接问题。随着其他库逐渐放弃渐进 API,因为它与 Promise 几乎没有关系,Bluebird 也会如此。可以使用类似于 IProgress 的模式来实现进度条的常见用例。在 C# 中。
之前使用过 jQuery:
Promise.resolve($.get(...))
.progressed(function() {
// ...
})
.then(function() {
// ...
})
.catch(function(e) {
// ...
})
之后使用 jQuery:
Promise.resolve($.get(...).progress(function() {
// ...
}))
.then(function() {
// ...
})
.catch(function(e) {
// ...
})
像 C# 一样实现通用进度接口(interface):
function returnsPromiseWithProgress(progressHandler) {
return doFirstAction().tap(function() {
progressHandler(0.33);
}).then(doSecondAction).tap(function() {
progressHandler(0.66);
}).then(doThirdAction).tap(function() {
progressHandler(1.00);
});
}
returnsPromiseWithProgress(function(progress) {
ui.progressbar.setWidth((progress * 200) + "px"); // update with on client side
}).then(function(value) { // action complete
// entire chain is complete.
}).catch(function(e) {
// error
});
关于javascript - ECMAScript Promise 中的进度通知,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32909694/