美文网首页JavaScript前端开发笔记
[ECMAScript] Promise相关的几个术语

[ECMAScript] Promise相关的几个术语

作者: 何幻 | 来源:发表于2017-07-18 11:35 被阅读62次

1. fulfilled,rejected,pending

A Promise is an object that is used as a placeholder for the eventual results of a deferred (and possibly asynchronous) computation.

Any Promise object is in one of three mutually exclusive states: fulfilled, rejected, and pending:

A promise p is fulfilled if p.then(f, r) will immediately enqueue a Job to call the function f.
A promise p is rejected if p.then(f, r) will immediately enqueue a Job to call the function r.
A promise is pending if it is neither fulfilled nor rejected.

一个promise总共有三种状态,pending,fulfilled/rejected,new Promise构造函数创建的promise,初始处于pending状态。

pending的时候不向Job Queue加入任何Job。

当一个promise p被fulfilled/rejected的时候,会立即向名字为PromiseJobs的Job Queue加入一个Job,这个Job做的事情就是调用p.then(f, r)中的f/r

2. settled,resolved

A promise is said to be settled if it is not pending, i.e. if it is either fulfilled or rejected.

A promise is resolved if it is settled or if it has been “locked in” to match the state of another promise. Attempting to resolve or reject a resolved promise has no effect. A promise is unresolved if it is not resolved. An unresolved promise is always in the pending state. A resolved promise may be pending, fulfilled or rejected.

2.1 settled

一个promise被settled,指的是它脱离了pending状态,或者被fulfilled或者被rejected。

2.2 resolved

resolve/reject一个promise,
指的是调用new Promise((res,rej)=>{...})中的res/rej函数。

调用了res函数之后的promise,称为resolved。

2.3 如何理解“locked in”

A promise is resolved if it is settled or if it has been “locked in” to match the state of another promise.

要想理解“locked in”,首先要知道,res函数可以接受三种类型的值作为参数,
或者是一个promise,或者是一个带有then方法的对象,或者是其他值。

这里“locked in”指的是res接受promise,或者接受带有then方法对象的情况,
这种方式进行resolve,会使得当前的promise仍然处于pending状态,
其settled结果,与其参数对象的最终状态有关。

例子:
(1)res的参数为promise的例子,

a = new Promise((res,rej)=>setTimeout(()=>res(1),1000));
b = new Promise((res,rej)=>res(a));

b.then(v=>console.log(v));

(2)res的参数为带then方法的对象的例子,

a = {
  then:(res,rej)=>setTimeout(()=>res(1),1000)
};
b = new Promise((res,rej)=>res(a));

b.then(v=>console.log(v));

2.4 多次resolve无效

Attempting to resolve or reject a resolved promise has no effect.
一个promise一旦resolved,再次resolve/reject将失效。

无论第一次resolve所使用的参数是什么,
例如,即使第一调用res仍然使promise处于pending状态。

a = new Promise((res,rej)=>{
  res({
    then:(res,rej)=>setTimeout(()=>res(1),1000)
  });

  res(2);

  setTimeout(()=>rej(3),1000);
});

a.then(
  v => console.log(v),
  e => console.warn(e)
);

3. Enqueue Job

3.1 何时p.then(f, r)会Enqueue Job

如果promise p还没有被settled,那么p.then(f, r),会将f/r放入promise的[[PromiseFulfillReactions]]/[[PromiseRejectReactions]]列表尾部。

如果p已经被settled,会向名为PromiseJobs的Job Queue中添加一个用来处理fulfilled/rejected的PromiseReactionJob

因此,一个已经settled的promise,每次调用p.then(f, r)会Enqueue Job。
[[PromiseFulfillReactions]][[PromiseRejectReactions]]的作用是,当promise处于pending状态时,保存p.then(f, r)所添加的处理器函数fr

3.2 使用res / rej Enqueue的两种Job

res会出现在以下两个地方:

(1)带有then方法的对象中,{then:(res,rej)=>{...}}
用一个带有then方法的对象调用res,会向名为PromiseJobs的Job Queue中添加一个PromiseResolveThenableJob

(2)new Promise((res,rej)=>{...})构造函数中
调用res/rej,会导致promise被settled,(只能被settle一次)
此时会清空promise的[[PromiseFulfillReactions]][[PromiseRejectReactions]]列表,并向名为PromiseJobs的Job Queue中添加一个用来处理fulfilled/rejected的 PromiseReactionJob

因此,调用res/rej总是会Enqueue Job。
当promise被settled的时候,[[PromiseFulfillReactions]][[PromiseRejectReactions]]就没有用了,因为settled状态的promise,即使再添加处理器p.then(f, r),也是直接Enqueue Job,和[[PromiseFulfillReactions]][[PromiseRejectReactions]]无关。

a = new Promise(res=>setTimeout(()=>res(1),1000));
a.then(v=>console.log(v));

setTimeout(()=>a.then(v=>console.warn(v)),2000);

参考

StackOverflow: What is the correct terminology for javascript promises
ECMAScript 2018 Language Specification

相关文章

  • [ECMAScript] Promise相关的几个术语

    1. fulfilled,rejected,pending A Promise is an object that...

  • JavaScript Promise 对象

    一、什么是Promise ECMAscript 6 提供了 Promise 对象。Promise 是异步编程的一个...

  • 会用不如会造 - Promise

    为什么要自己造 Promise Promise已经是Ecmascript 6标准中的内容 Promise是asyn...

  • Promise.resolve()

    参考文档:ECMAScript 6入门 Promise.resolve()用于将现有对象转换为Promise对象,...

  • ES6 Promise 详解

    ECMAscript 6 原生提供了 Promise 对象。 Promise 对象代表了未来将要发生的事件,用来传...

  • 2018-11-07

    《ECMAScript 6》学习笔记 参考:阮一封ECMAScript 6 一、Promise对象 1. stac...

  • ECMAScript之Promise

    学习Promise前我们先搞清楚这几个单词。 promise,resolve,reject => 承诺,解决,失败...

  • ECMAScript Promise对象

    Promise的含义 Promise ,简单说就是一个容器,里面保存着某个未来才会结束的事件(通常是一个异步操作)...

  • ECMAScript 6 Promise

    ES6 规定,Promise对象是一个构造函数,用来生成Promise实例。 Promise对象有以下两个特点。 ...

  • JS----promise

    Promise 对象代表了未来将要发生的事件,用来传递异步操作的消息Promise是ECMAscript 6 的一...

网友评论

    本文标题:[ECMAScript] Promise相关的几个术语

    本文链接:https://www.haomeiwen.com/subject/lwpckxtx.html