javascript ECMAScript Promise 中的进度通知

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32909694/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 15:54:16  来源:igfitidea点击:

progress notifications in ECMAScript Promise

javascriptpromiseecmascript-6

提问by Assaf Shemesh

We're using ECMAScript 6 promises.

我们正在使用ECMAScript 6 promises.

We need to implement progress notifications to the end-user (this is pure UX requirement). I know that other promise frameworks (Q promiselibrary, for ex.) allows that.

我们需要向最终用户实施进度通知(这是纯粹的 UX 要求)。我知道其他承诺框架(例如Q promise库)允许这样做。

How can we adopt some kind of progress indication most elegantly?

我们如何才能最优雅地采用某种进度指示?

Or should we migrateto a different framework? (I don't know how to estimate the effort of the latter)

还是我们应该迁移到不同的框架?(我不知道如何估计后者的努力)

采纳答案by Benjamin Gruenbaum

ES2015 promises will never have progression. Promises represent a singular eventual value. If you want multiple values you can look into observables - or put the progress on the promise returning function.

ES2015 承诺永远不会有进展。Promises 代表一个单一的最终值。如果您想要多个值,您可以查看 observables - 或者将进度放在 promise 返回函数上。

Putting the progress on the promise returning function is pretty easy. Basically you take a callback as a parameter to the function and call it whenever a progress notification should happen.

将进度放在 Promise 返回函数上非常容易。基本上,您将回调作为函数的参数,并在发生进度通知时调用它。

Here is some text adapted from our guide at bluebird:

以下是改编自我们在bluebird 上的指南的一些文字:

Progression has composability and chaining issues with APIs that use promise progression handlers. As other libraries move away from the progression API since it really has little to do with promises, so will Bluebird. Implementing the common use case of progress bars can be accomplished using a pattern similar to IProgressin C#.

Progression 与使用 promise 进程处理程序的 API 存在可组合性和链接问题。随着其他库远离progression API,因为它实际上与promise 关系不大,Bluebird 也将如此。可以使用类似于C# 中的IProgress的模式来实现进度条的常见用例。

Using jQuery before:

之前使用 jQuery:

Promise.resolve($.get(...))
    .progressed(function() {
        // ...
    })
    .then(function() {
        // ...
    })
    .catch(function(e) {
        // ...
    })

Using jQuery after:

之后使用 jQuery:

Promise.resolve($.get(...).progress(function() {
        // ...
    }))
    .then(function() {
        // ...
    })
    .catch(function(e) {
        // ...
    })

Implementing general progress interfaces like in C#:

在 C# 中实现通用的进度接口:

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
});