什么是承诺对象? [英] What is a promise object?

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

问题描述

在开始学习Ember之后,只要有对Promise Object的引用,我就会感到困惑。我知道对象,例如类X或JSON对象的实例。例如,当有人说方法 m 返回JSON对象时,我知道要预期键值对。如何将这种理解与承诺对象联系起来?是一个promise对象的值(可以是数组,字符串,数字,JSON对象等)?



如果一个方法返回一个promise对象,我应该期望什么? / p>

解决方案

承诺是EcmaScript 6(ES6)的新对象类型,其中有许多 polyfills和libaries (即ES5 JavaScript引擎的实现),并允许(以及其他优点)摆脱臭名昭着的回调地狱em>,并且易于写入和读取异步代码。



承诺可以拥有以下三种状态之一(只有一种):




  • 待定

  • 履行

  • 拒绝



如果承诺被拒绝或实现,它也有一个 / strong>状态。



基本上,它是一个具有然后属性(其他s),它是一个函数,它至少需要一个函数作为参数,并且可以采用两个函数:如果承诺返回满足的,则第一个将被调用状态,如果承诺返回拒绝状态,则将调用第二个状态



然后 函数返回自己的另一个承诺,所以承诺是可链接的。



承诺对象比我刚才在这里写的复杂得多,但只是给你一个开始。



BTW,如果你使用jQuery,你可能已经使用了一个类似promise的对象(注意样的后缀) : $。ajax()返回一个promise样对象(这些对象被称为可变),它有一个 / code>(和一个然后)属性,它是一个函数,它接受一个函数作为参数看起来像一个履行函数(通常只需要一个参数)。承诺对象也可能有一个完成函数属性(不标准化,AFAIK,但几乎所有的polyfills和库实现它),它的作用就像一个then函数,只有它没有退回承诺(因此名称:如果您完成了承诺,则使用 done(),但是如果您需要做出某些承诺的结果,使用 then())。



例如:你可能已经看过或写过这样的东西:

  $。ajax({url:'/ path / to / html / piece'})
.done(function(data) {
$('whateverSelector')。html(data);
});

但是jQuery调用的承诺,即使它们是可以,那么不符合承诺规范


Having started learning Ember, I get confused whenever there is a reference to Promise Object. I am aware of objects e.g. instance of class X or a JSON object. For instance, when somebody says that method m returns a JSON object then I know to expect key-value pairs. How do I relate this understanding to promise objects? Is a promise object a value (could be array, string, number, JSON object, etc.)?

What should I expect if a method returns a promise object?

解决方案

A promise is a new Object type of EcmaScript 6 (ES6), for which there are numerous polyfills and libaries (i.e. implementations for ES5 JavaScript engines), and allows (among other benefits) to get out of the infamous callback hell, and to write and read asynchronous code easily.

A promise can have one (and only one) of these three status:

  • pending
  • fulfilled
  • rejected

If the promise is rejected or fulfilled, it also has a settled status.

Basically, it is an object that has a then property (amongst others), which is a function that takes at least one function as a parameter, and can take two: the first one will be invoked if the promise returns a fulfilled status, and the second one will be invoked if the promise returns a rejected status

The then function returns itself another promise, so promises are chainable.

Promise objects are rather more complicated than what I just wrote here, but it was just to give you a start.

BTW, you may have used a promise-like object (note the -like suffix) if you use jQuery : $.ajax() returns a promise-like object (those are called thenables) that has a done (and a then) property which is a function that accepts a function as the parameter that seems like a fulfilled function (which normally takes only one argument). Promise objects also may have a done function property (not standardised, AFAIK, but almost all the polyfills and the libraries implement it), which acts like a then function, only it does not return a promise (hence the name: if you are done with the promise, then use done(), but if you need to do somehting with the result of the promise, use then()).

e.g.: you may have seen or written something like this:

$.ajax({url: '/path/to/html/piece'})
    .done(function(data) {
        $('whateverSelector').html(data);
    });

But what jQuery calls promises, even if they are thenables, does not fulfill the promise spec.

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

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