什么时候有人需要创建延期? [英] When would someone need to create a deferred?

查看:124
本文介绍了什么时候有人需要创建延期?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通常来说,创建延迟对象通常不鼓励使用ES6风格的Promise构造函数。有没有必要(或者仅仅是以某种方式)使用延期的情况?



例如,在 this page ,以下示例作为使用deferred的理由:

 函数delay(ms){
var deferred = Promise.pending();
setTimeout(function(){
deferred.resolve();
},ms);
return deferred.promise;
}

然而,这也可以和Promise构造函数一样完成: p>

 函数delay(ms){
return new Promise(function(resolve,reject)){
setTimeout (){
resolve();
},ms);
});
}


解决方案


有没有必要(或只是
更好的某种方式)使用延期的情况?


没有必要延期的情况。 更好是一个意见问题,所以我不会在这里解决。



有一个原因,ES6承诺规范没有延迟对象。 你根本不需要一个。使用延迟对象的任何东西可以始终是另一种不使用延迟对象的方式。



首先,大部分用途非常适合承诺构造函数模型。其次,任何其他不太适合这种模式的情况,仍然可以用诺言的构造者来完成,或者从解决的承诺开始,并链接到它。



我看到的延期的主要用例是当你想传递到 resolve() reject()除了创建承诺的代码之外的其他一些代码。一个延迟使得它非常容易,因为你可以通过延迟对象,它有公共方法来解决或拒绝它。但是,有一个承诺,你也可以通过解决方案和/或拒绝方法。由于它们自动绑定到特定对象,所以您只需传递函数引用。而且,在其他情况下,您可以让其他代码创建自己的承诺,并自行解决/拒绝该操作,并将该操作与您的操作相关联,而不是让它们解决/拒绝您的承诺。是否像传递延迟对象那样干净?主要是一个意见的问题,但既不是非常常见的用例,而且都是没有单独延期的对象可以实现的。



而且,如torazaburo指出,一些外部代码解决或拒绝你的承诺是一个自己的反格局。你创造了承诺 - 你解决/拒绝它。如果您想使用外部事件来决定何时解决/拒绝它,那么让他们通过自己的承诺或回调通知您,您可以解决/拒绝您自己的承诺。或者,让他们创造自己的承诺,你可以使用。这真的是所需的模式。或者,让他们链接到你的承诺,以便最终的结果是在他们的操作完成时被门控。



如果用来编码延迟对象(说一个jQuery延迟),没有它可能需要一点点的编码,但是在一段时间之后,你只是开始思考不同,它开始自然地使用promise构造函数。



一旦您承诺在任何给定的应用程序中使用的异步操作的领域,您甚至不需要再创建自己的承诺是非常罕见的,因为您几乎只是构建异步功能的承诺呼叫已经创建或使用流量控制操作,如 Promise.all(),它们为您创建了一个应用程序。



这是反模式的要点。 使用已经为您创建的承诺,而不是手动创建更多。链接它们。从 .then()处理程序返回承诺,以在逻辑控制下链接异步操作。



当然,现有异步操作不会返回有人需要创建承诺的承诺,但是应该在主编码逻辑的提供之外的promisify层中完成,并且仅对该操作执行一次,然后才调用异步操作的代码应该能够使用退回的承诺。


It seems generally that creating deferred objects is now commonly discouraged in favor of using the ES6-style Promise constructor. Does there exist a situation where it would be necessary (or just better somehow) to use a deferred?

For example, on this page, the following example is given as justification for using a deferred:

function delay(ms) {
    var deferred = Promise.pending();
    setTimeout(function(){
        deferred.resolve();
    }, ms);
    return deferred.promise;
}

However, this could be done just as well with the Promise constructor:

function delay(ms) {
    return new Promise(function(resolve, reject){
        setTimeout(function(){
            resolve();
        }, ms);
    });
}

解决方案

Does there exist a situation where it would be necessary (or just better somehow) to use a deferred?

There is no such situation where a deferred is necessary. "Better" is a matter of opinion so I won't address that here.

There's a reason that the ES6 promise specification does not have a deferred object. You simply don't need one. Anything that people used to use a deferred object for can always be done another way that doesn't use a deferred object.

First off, the majority of uses fit very nicely into the promise constructor model. Secondly, any other cases that didn't fit quite so cleanly into that model can still be accomplished with the promise constructor or by starting with a resolved promise and chaining to it.

The main use case I've seen for a deferred is when you want to pass the ability to resolve() or reject() off to some other piece of code other than the code that created the promise. A Deferred made that very easy since you could just pass the deferred object and it had public methods for resolving or rejecting it. But, with a promise, you can also just pass the resolve and/or reject methods. Since they are bound to the specific object automatically, you can just pass the function references. And, in other cases, you can just let the other code create their own promise and resolve/reject it themselves and have that operation linked to yours rather than letting them resolve/reject your promise. Is all that quite as clean as passing a deferred object? Mostly a matter of opinion, but neither are very common use cases and all are something that can be accomplished without a separate deferred object.

And, as torazaburo points out, letting some external code resolve or reject your promise is a bit of an anti-pattern in its own right. You created the promise - you resolve/reject it. If you want to use external events to decide when to resolve/reject it, then have them notify you (via their own promise or callback) and you can resolve/reject your own promise. Or, have them create their own promise that you can use. That's really the desired model. Or, let them chain onto your promise so that the end result is gated by when their operation is done.

If one was used to coding with the deferred object (say with a jQuery deferred), it might take a little getting used to coding without it, but after a little while you just start to think differently and it starts to come natural to just use the promise constructor.

Once you promisify the sphere of async operations that you use in any given application, it's pretty rare that you ever even need to create your own promises any more because you are mostly just building off promises that the async functions you call are already creating or using flow control operations like Promise.all() which create uber promises for you.

This is the main point of anti-patterns. Use the promises that are already created for you rather than manually create more. Chain them. Return promises from .then() handlers to link async operations under logic control.

Of course, there are existing async operations that don't return promises for which somebody needs to create a promise, but that should be done in a promisify layer somewhere outside the purvey of your main coding logic and done only once for that operation and then code that calls the async operation should be able to just use the returned promises.

这篇关于什么时候有人需要创建延期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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