延期与承诺 [英] Deferred versus promise

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

问题描述

除了jQuery版本以外,Deferred和Promise有什么区别?

What is the difference between Deferred and Promise other than the jQuery versions?

我该怎么用?我只想调用fooExecute().例如,我只需要fooStart()fooEnd()来切换html div状态.

What should I use for my need? I only want to call the fooExecute(). I only need the fooStart() and fooEnd() to toggle the html div status for example.

//I'm using jQuery v2.0.0
function fooStart() { /* Start Notification */ }
function fooEnd() { /* End Notification */ }
function fooExecute() { /* Execute the scripts */ }

$('#button1').on('click', function() {
    var deferred1 = $.Deferred();
    var promise1 = $.Promise();

    deferred1.???

    promise1.???
});

推荐答案

首先:您不能使用$.Promise();,因为它不存在.

First: You cannot use $.Promise(); because it does not exist.

延期对象是可以创建承诺并将其状态更改为resolvedrejected的对象.如果您编写自己的函数并希望对调用代码提供承诺,则通常使用Deferreds.您是该值的生产者.

A deferred object is an object that can create a promise and change its state to resolved or rejected. Deferreds are typically used if you write your own function and want to provide a promise to the calling code. You are the producer of the value.

顾名思义,承诺是对未来价值的承诺.您可以在其上附加回调以获取该值.诺言已兑现"给您,您是未来价值的接受者.
您无法修改承诺的状态.只有创建承诺的代码才能更改其状态.

A promise is, as the name says, a promise about future value. You can attach callbacks to it to get that value. The promise was "given" to you and you are the receiver of the future value.
You cannot modify the state of the promise. Only the code that created the promise can change its state.

1. (生产)当您想为自己的功能提供承诺支持时,可以使用延迟对象.您需要计算一个值,并希望控制何时兑现承诺.

1. (produce) You use deferred objects when you want to provide promise-support for your own functions. You compute a value and want to control when the promise is resolved.

function callMe() {
    var d = new $.Deferred();
    setTimeout(function() {
        d.resolve('some_value_compute_asynchronously');
    }, 1000);
    return d.promise();
}

callMe().done(function(value) {
    alert(value);
});

2. (转发).如果您要调用的函数本身返回一个Promise,则不必创建自己的延迟对象.您可以兑现承诺.在这种情况下,该函数不会创建值,而是将其转发(类似):

2. (forward) If you are calling a function which itself returns a promise, then you don't have to create your own deferred object. You can just return that promise. In this case, the function does not create value, but forwards it (kind of):

function fetchData() {
    // do some configuration here and pass to `$.ajax`
    return $.ajax({...});
}

fetchData().done(function(response) {
    // ...
});

3. (接收)有时您不想创建或传递承诺/值,而是想直接使用它们,即您是某些信息的接收者:

3. (receive) Sometimes you don't want to create or pass along promises/values, you want to use them directly, i.e. you are the receiver of some information:

$('#my_element').fadeOut().promise().done(function() {
    // called when animation is finished
});

当然,所有这些用例也可以混合使用.您的函数可以是值的接收者(例如,通过Ajax调用),并以此为基础计算(产生)一个不同的值.

Of course, all these use cases can be mixed as well. Your function can be the receiver of value (from an Ajax call for example) and compute (produce) a different value based on that.

相关问题:

  • What are the differences between Deferred, Promise and Future in JavaScript?
  • What's the difference between a Deferred object and its own promise object?

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

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