延迟/承诺混乱和实施 [英] Deferred/promise confusion and implementation

查看:117
本文介绍了延迟/承诺混乱和实施的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我想自己实施延迟,这是否是正确的方法(试图了解内部逻辑):

延期算作一种行为模式吗?

延期和承诺有什么区别?

function Deferred() {
    var d = {},
        f = {},
        a = {}, 
        state = 'pending';

    return {
        resolve: function(){
            state = 'resolved';
            a.fn.apply(a.context, arguments);
            d.fn.apply(d.context, arguments);
        },

        reject:  function(){
            state = 'rejected';
            a.fn.apply(a.context, arguments);
            f.fn.apply(f.context, arguments);
        },

        done: function(fn, context) {
            d = {fn: fn, context: context};
            return this;
        },

        fail: function(fn, context) {
            f = {fn:fn, context: context};
            return this;
        },

        always: function(fn, context) {
            a = {fn:fn, context: context};
            return this;
        },

        state: state
    }
}

应用示例:

var obj = Deferred();
    obj.done(function(arg){
        console.log('we are done here. why? -', arg);
    }, window)
     .always(function(arg){
        console.log('print that in any case. and some details:', arg);
    }, window)
     .fail(function(arg){
        console.log('we failed here. why? -', arg);
    }), window;


    obj.reject('some arguments');
    obj.resolve({argument: 'hooray!'});

解决方案

如果我想自己实施延迟,这是正确的方法吗?

不,您的代码缺少一些要点:

  • 承诺/延期仅代表一个单一的结果.结算(完成或拒绝)后,不得更改其状态.
  • 无论何时安装回调,都会尽快执行回调并返回结果.结果到达后,您的代码将无法执行此操作.
  • 无论安装了多少个回调,都将执行它们.您的代码只允许存储一个回调.

您可能想看看此示例实现 Promises中进行了描述/A +规范.

延期算作一种行为模式吗?

是的,您可以将它们视为观察者或访客模式的组合.

延期和承诺有什么区别?

通常,延迟的确提供了实现或拒绝的方法,而promise是用于安装回调的接口(无法解析promise).有关详细信息,请参见 JavaScript中的Deferred,Promise和Future之间有什么区别?.

If I would like to implement deferred by myself, would it be a right way to do(trying to understand the internal logic):

Does deferred counts as a behavioural pattern?

What is the difference between deferred and promise?

function Deferred() {
    var d = {},
        f = {},
        a = {}, 
        state = 'pending';

    return {
        resolve: function(){
            state = 'resolved';
            a.fn.apply(a.context, arguments);
            d.fn.apply(d.context, arguments);
        },

        reject:  function(){
            state = 'rejected';
            a.fn.apply(a.context, arguments);
            f.fn.apply(f.context, arguments);
        },

        done: function(fn, context) {
            d = {fn: fn, context: context};
            return this;
        },

        fail: function(fn, context) {
            f = {fn:fn, context: context};
            return this;
        },

        always: function(fn, context) {
            a = {fn:fn, context: context};
            return this;
        },

        state: state
    }
}

Application example:

var obj = Deferred();
    obj.done(function(arg){
        console.log('we are done here. why? -', arg);
    }, window)
     .always(function(arg){
        console.log('print that in any case. and some details:', arg);
    }, window)
     .fail(function(arg){
        console.log('we failed here. why? -', arg);
    }), window;


    obj.reject('some arguments');
    obj.resolve({argument: 'hooray!'});

解决方案

If I would like to implement deferred by myself, would this be a right way to do?

No, your code misses a few important points:

  • A promise/deferred represents one single outcome only. It's state must not be changed after it is settled (fulfilled or rejected).
  • No matter when a callback is installed, it will be executed as soon as possible with the result. Your code fails to do that when the result has already arrived.
  • No matter how many callbacks are installed, they all will be executed. Your code allows to store only a single callback.

You might want to have a look at this example implementation or How is a promise/defer library implemented?.

Also, for a promise you will want to implement an interoperable then method for chaining, whose behaviour is described in the Promises/A+ specification.

Does deferred counts as a behavioural pattern?

Yeah, you could count them as a mix of observer or visitor pattern.

What is the difference between deferred and promise?

In general, a deferred does provide the methods to fulfill or reject, while a promise is the interface to install the callbacks on (without the ability to resolve the promise). See What are the differences between Deferred, Promise and Future in JavaScript? for details.

这篇关于延迟/承诺混乱和实施的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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