探索诺言关于诺言的2种可能被忽略的观点

This is part 2 of my exploring on promise. In part 1, I shared my thoughts about “async” and “event loop” as the basis to better understand promise. The main purpose of this part is share some key points or say “blind spots” about promise that may impede your understanding of promise.

这是我探索承诺的第二部分。 在第1部分中,我分享了我对“异步”和“事件循环”作为更好理解诺言的基础的想法。 这部分的主要目的是分享一些关于承诺的关键点或说“盲点”,这可能会妨碍您对承诺的理解。

After a brief introduction about basic aspects of promise, I’ll share a few links for learning how to use promise. Because have a basic sense about what is promise and how to use it is important for the main discussion in this article. You don’t have to master promise after the studies, otherwise there wouldn’t have been this article. I believe many beginners will leave mental gaps after being introduced with promise. Some key points are somehow omitted by most learning materials. Maybe they are too obvious to pros, butnot so obvious to newbies. It’s more of a communication problem. I hope this article can help you recognize a few of these points and help you connect the dots from “async” to “promise”.

在简要介绍了诺言的基本方面之后,我将分享一些链接以学习如何使用诺言。 因为对承诺和如何使用有基本的了解对于本文的主要讨论很重要。 在学习之后,您不必掌握诺言,否则就不会有本文了。 我相信,许多初学者在有前途的介绍之后会留下精神鸿沟。 大多数学习材料都以某种方式省略了一些关键点。 也许对专业人士而言,它们太明显了,但是对新手而言却不是那么明显。 这更多的是沟通问题。 我希望本文可以帮助您认识到其中的一些要点,并帮助您将“异步”到“承诺”之间的点点滴滴联系起来。

Terms in this article

本文中的术语

Based on different contexts, the word “promise” has different meanings, most of the difference can be distinguished with different writing forms but there’re a few subtle ones may not be easily distinguished. In this post “promise” may in the forms of:

根据不同的上下文,“承诺”一词具有不同的含义,大多数差异可以通过不同的书写形式加以区分,但是有一些细微之处可能难以区分。 在这篇文章中,“承诺”可以采用以下形式:

  • plain lowercase “promise”: the general concept of promise

    普通的小写“承诺”:promise的一般概念
  • code quoted lowercase promise: an instance of a promise

    代码引用小写的promisepromise一个实例

  • code quoted uppercase Promise: the Promise constructor

    用大写字母Promise引用的代码: Promise 构造函数

And resolve(d) and fulfill(ed) are used interchangeably.

并且resolve(d)fulfill(ed)可互换使用。

1关于承诺的基本方面 (1 Basic aspects about promise)

1.1承诺感 (1.1 Sense of promise)

I want to start with different definitions of promise. For now we don’t have to understand all the terms before we can continue. Here comes the definitions:

我想从promise的不同定义开始。 目前,我们不必先理解所有术语,然后再继续。 定义如下:

  • Promise/A+: A promise represents the eventual result of an asynchronous operation. The primary way of interacting with a promise is through its then method, which registers callbacks to receive either a promise’s eventual value or the reason why the promise cannot be fulfilled.

    Promise / A +Promise / A +表示异步操作的最终结果。 与承诺进行交互的主要方式是通过其then方法,该方法注册回调以接收承诺的最终值或无法实现承诺的原因。

  • MDN: A Promise is a proxy for a value not necessarily known when the promise is created. It allows you to associate handlers with an asynchronous action’s eventual success value or failure reason.

    MDN :Promise是创建Promise时不一定知道的值的代理。 它允许您将处理程序与异步操作的最终成功值或失败原因相关联。

  • wikipedia: In computer science, future, promise, delay, and deferred refer to constructs used for synchronizing program execution in some concurrent programming languages. They describe an object that acts as a proxy for a result that is initially unknown, usually because the computation of its value is not yet complete.

    维基百科 :在计算机科学中,future,promise,delay和deferred指的是用于在某些并发编程语言中同步程序执行的结构。 它们描述了一个对象,该对象充当最初未知的结果的代理,通常是因为其值的计算尚未完成。

So promise must have something to do with “async” tasks, and it’s a representation/proxy for a future result. Bringing this high level sense of promise into the exploring promise is necessary.

因此,promise必须与“异步”任务有关,并且它是将来结果的表示/代理。 必须将这种高度的诺言感带入探索的诺言中。

1.2使用诺言 (1.2 use of promise)

As I said, this part of work(use of promise) is excellently done by some pros, thank them a lot!

正如我所说,这部分工作(使用诺言)是由一些专业人士出色完成的,非常感谢他们!

This article does a thorough explanation about the use of promise with code examples, along with some performance concerns. Inevitably you would come across some unacquainted terms. You can glimpse their definitions on wiki if you want to, but don’t go too deep, focus on “how to use promise” and just get a feel about it. And you may want to read it multiple times as I did.

本文对代码示例中对promise的使用以及一些性能方面的问题进行了详尽的解释。 不可避免地,您会遇到一些陌生的术语。 如果愿意,您可以在Wiki上看到它们的定义,但不要太深入,只关注“如何使用Promise”,并对此有所了解。 您可能想要像我一样阅读多次。

1.3承诺状态 (1.3 states of promise)

Promise is like a wrapper for asynchronous operations(tasks), and it holds the result of the task and based on how things are going, it stipulates a promise can be in one of three states:

Promise就像异步操作(任务)的包装器一样,它保存任务的结果,并根据事情的发展情况,规定promise可以处于以下三种状态之一

  • pending: the initial state, means the task is still processing and we don’t know how things are going so far

    待定: 初始状态 ,表示任务仍在处理中,我们不知道到目前为止情况如何

  • resolved/fulfilled: means the task is successfully fulfilled, and it may give us something we want such as data or just a message that indicates the task has succeeded.

    已解决/已完成:表示任务已成功完成,并且可能为我们提供了一些我们想要的东西,例如数据或仅一条消息,表明任务已成功完成。
  • rejected: means the task failed, and reasonably a reason(often an error object) should be given to tell what was wrong

    拒绝:表示任务失败,并且应该合理地给出原因(通常是错误对象)以说明错误所在

pending is when the async operation is still processing, resolved(fulfilled) and rejected are when the async operation is completed whether succeeded or failed, when a promise’s state is resolved(fulfilled) or rejected, we also say it’s settled.

pending是指异步操作仍在处理中,已resolved(fulfilled)和已rejected是指异步操作完成(无论成功还是失败), promise状态已resolved(fulfilled)或已rejected ,我们也说它已解决

2一些可能被忽略的关键点 (2 A few key points that may be overlooked)

This part mainly shares with you some key points about promise. They are not overlooked by purpose, and you may feel so strange that you haven’t noticed them. Because they are just some basic facts sit there for a long time.

这部分主要与您分享有关承诺的一些关键点。 它们不是故意被忽略的,您可能会感到很奇怪,以至于您还没有注意到它们。 因为它们只是一些基本事实,因此已经存在了很长时间。

2.1 Promise构造函数用于创建Promise,then()方法用于访问Promise (2.1 Promise constructor is used for creating promise, then() method is used for accessing promise)

There’s a concise description about the purpose of Promise constructor.

关于Promise构造函数的用途有一个简洁的描述。

The Promise constructor is primarily used to wrap functions that do not already support promises.

Promise构造函数主要用于包装不支持promise的函数。

After reading a lot about how to use promise, we know that Promise() can create a promise and then is the way to chain subsequent operations. But being aware of the original designing purpose is also important, especially when you ask question like “Since Promise() and then() both return a promise, so what’s the difference?” . Maybe we should ask a more basic question: what a constructor is used for in JavaScript?

读了很多关于如何使用的承诺后,我们知道, Promise()可以创建一个promisethen是通向链后续操作。 但是,了解原始设计目的也很重要,尤其是当您问诸如“因为Promise()then()都返回诺言,那么有什么区别?”之类的问题时,尤其如此。 。 也许我们应该问一个更基本的问题:JavaScript中使用了什么构造函数?

The answer is when we want to create a promise, the Promise() constructor is the first choice, not then().We can say Promise() is primarily used to wrap functions that do not already support promises. Or we can say it’s used for “Promisifying” something. And then() is the way to chain promises, as well as the way to access the value of a promise. Though then() always returns a promise , we should not treat this behavior as its designing purpose. Seeing then() as the interface to access promises is a more appropriate view.

答案是,当我们要创建一个promise ,该Promise()构造函数是第一选择,而不是then()我们可以说Promise()主要用于包装不已经支持承诺的功能。 或者我们可以说它用于“承诺”某些东西。 then()是链接promise的方法,也是访问promise价值的方法。 尽管then()总是返回一个promise ,但我们不应将此行为视为其设计目的。 将then()视为访问promises的接口是一个更合适的视图。

Promise中的2.2代码在创建Promise后立即执行 (2.2 code in Promise executes as soon as the promise is created)

I think this is an important fact but most intro level materials don’t mention. And this trapped me for a long time when I was trying to figure out how to use promise.

我认为这是一个重要的事实,但是大多数入门级材料都没有提及。 当我试图弄清楚如何使用Promise时,这困扰了我很长时间。

the beginning of creation is the beginning of executing

创建的开始就是执行的开始

If we have a function that returns a promise:

如果我们有一个返回promise的函数:

function makePromise() {
new Promise((resolve, reject) => {
// do sync thing one
// do sync thing two
// resolve or reject at a certain point
})
};

When you execute makePromise(), thing one and thing two in the callback are beginning execution and are done synchronously immediately. I don’t know why I had a tendency(don’t know if others have too) to think all the code within the Promise() constructor only begins executing as a whole at the settling point, the point when resolve or reject are called. Realizing this is important for us to maintain the execution sequence of tasks and thinking about possible performance considerations.

当执行makePromise() ,回调中的第一thing one和第二thing two开始执行,并立即同步完成。 我不知道为什么我倾向于(不知道其他人是否也有这种想法)认为Promise()构造函数中的所有代码仅在沉降点(称为resolvereject的点Promise()开始整体执行。 意识到这一点对于我们维持任务的执行顺序并考虑可能的性能考虑很重要。

order of creation is not the guarantee of order of completion

创建的顺序不是完成顺序的保证

If we have a list of urls [u1, u2, u3] that don’t depend on each other, means they can be loaded in parallel. But we want to get things from the 3 urls one after another, in the order of 1,2,3. We may write something like this:

如果我们有一个相互不依赖的URL [u1, u2, u3]列表,则意味着它们可以并行加载。 但是我们想要从三个网址中依次获取内容,顺序为1,2,3. 我们可以这样写:

function requestURL(url) {
return new Promise((resolve, reject) => {
let xhr = new XMLHttpRequest();
xhr.open('GET', url);
xhr.addEventListener('load', () => {
let result = xhr.response;
resolve(result);
})
});
};[u1, u2, u3].forEach(url => {
requestURL(url)
})

Although all the requests may succeed but the order of completion is not guaranteed. Why? Because forEach is sync and what we actually did can be seen as:

尽管所有请求都可能成功,但是不能保证完成的顺序。 为什么? 因为forEach是同步的,所以我们实际上可以将其视为:

requestURL(u1);
requestURL(u2);
requestURL(u3);

All promises begin creating almost at the same time because the 3 function calls are executed synchronously, meanwhile all code within Promise() constructor begins executing. So the 3 requests begins at almost the same time but we don’t know how much time each request would take, therefore we don’t know the order completion.

所有promise齐读同时几乎创造,因为3个函数调用中同步执行,同时所有代码Promise()构造函数开始执行。 因此这3个请求几乎在同一时间开始,但是我们不知道每个请求要花费多少时间,因此我们不知道订单完成情况。

there’s no waiting among multiple promises created independently

在独立创建的多个承诺中没有等待

Since a promise chain will be paused for pending promises, it’s easy to transfer this fact(feeling) to the situation when we create multiple promises at one time, thinking that lately created promises would wait for the earlier ones to be settled. But:

由于承诺链将暂停以pending承诺,因此很容易将这一事实(感觉)转移到当我们一次创建多个promise时的情况,并认为最近创建的promise将等待较早的promises被解决。 但:

  • waiting happens when there’s pending promise in a chain. You can’t just make a promise independently then “pause” it there, neither from inside nor outside.

    链中有未完成的承诺时,就会发生pending 。 您不能只是独立地做出一个promise然后在内部或外部“暂停”它。

  • a pending promise never pauses itself. When a promise is created, its original state is pending, but from an internal view, pending doesn’t mean pausing/waiting. As long as there is call for resolve or reject inside the callback passed to Promise() constructor, a pending promise will be approaching the state of fulfilled or rejected.

    pending兑现的承诺永远不会暂停。 创建诺言后,其原始状态为pending ,但是从内部角度来看, pending并不意味着暂停/等待。 只要在传递给Promise()构造函数的回调中有resolvereject的调用, pending Promise()就会接近已fulfilled或已rejected的状态。

So creating a bunch of promises doesn’t mean the latter ones will wait for the earlier ones, doesn’t mean they be completed in the order of creation. Unless you wrap the process of creating promise inside a function(a function returns a promise), then arrange them in a chain. There is a big difference between “creating a promise” and “a function that creates a promise”. Because when we pass “a function that creates a promise” to then(), the creation of promise won’t start before the chain advances to that then().

因此,创建一堆promise s并不意味着后一个promise将等待较早的promise ,并不意味着它们将按照创建顺序完成。 除非您将创建promise的过程包装在一个函数内(一个函数返回promise ),否则将它们安排在一个链中。 “创造承诺”和“创造承诺的功能”之间有很大的区别。 因为当我们将“创建诺言的函数”传递给then() ,诺言的创建不会在链前进到then()之前开始。

how to maintain sequence of operations

如何保持操作顺序

How to chain the requests in a wanted sequence or say initiate them one after another? Also with forEach, but this time a bit different.

如何以所需顺序链接请求,或者说一个接一个地发起它们? 同样适用于forEach ,但这一次有所不同。

let chain = Promise.resovle('');[u1, u2, u3].forEach(url => {
chain = chain.then(() => requestURL(url));
})

Notice chain.then(() => requestURL(url)) is different from chain.then(requestURL(url)), requestURL(url) is a function invocation that will create a promise immediately, you should always pass function to then().

注意chain.then(() => requestURL(url))chain.then(requestURL(url))requestURL(url)是一个函数调用,它将立即创建一个promise ,您应该始终将函数传递给then()

2.3 resolve()立即发生 (2.3 resolve() happens immediately)

The same example:

相同的例子:

function fetchURL(url) { // returns promise
return new Promise((resolve, reject) => {
let xhr = new XMLHttpRequest();
xhr.open('GET', url);
xhr.addEventListener('load', () => {
let suburl = xhr.response[0];
resolve(suburl);
})
});
};

This is a tricky point. The resolve method don’t know how much time a request would take. We call resolve in Promise constructor, and that happens inside the ‘load’ event listener. Here resolve(suburl) has no notion about sync/async it’s called immediately when the request is ’load’ed, and calling resolve(suburl) grants the state fulfilled to the promise with suburl as its value to prepare for possible future operations. And resolving of a promise is synchronous or say happens instantly.

这是一个棘手的问题。 resolve方法不知道请求将花费多少时间。 我们在Promise构造函数中调用resolve ,它发生在'load'事件监听器中。 在这里, resolve(suburl)没有关于sync/async概念,当请求被'load'时立即调用它,并且调用resolve(suburl)suburl作为其值来fulfilled承诺的承诺状态,以准备将来可能的操作。 承诺的解决是同步的,或者说是立即发生的。

This may seem obvious after you’ve noticed it. But realizing this fact can fill some mental gaps while trying to understand the using of promise. Since promise is heavily about “async”, it’s easy to forget that there’re also “sync” things there. It’s easy to grumble questions like “how does the promise know when to resolve itself?”, the answer is it doesn’t know. Because the “resolving” moment depends on something else such as explicit writing sync code to resolve the promise, like Promise.resolve() or call resolve in a Promise constructor.

在您注意到之后,这似乎显而易见。 但是认识到这一事实可以填补一些精神上的空白,同时试图理解诺言的使用。 由于Promise很大程度上与“异步”有关,因此很容易忘记那里也存在“同步”事物。 很容易抱怨诸如“诺言如何知道何时解决自己?”之类的问题,答案是它不知道。 因为“解决”时刻取决于其他事情,例如显式编写同步代码来解决承诺,例如Promise.resolve()Promise构造函数中的调用resolve

To me “resolve() happens immediately” is a very useful nonsense.

对我来说,“ resolve()立即发生”是一个非常有用的废话。

2.4功能是承诺链中的唯一货币 (2.4 function is the only currency within a promise chain)

I think initially we all know that then takes functions as arguments after we learned about the definition and use of promise. But as days roll on, we may want to stuff anything inside that pair of parentheses () followed by then. Especially things that are not function.

我认为起初我们都知道, then我们在了解了Promise的定义和使用之后then函数作为参数。 但是随着时间的流逝,我们可能想将任何东西塞入该括号()then填充then 。 尤其是那些不起作用的东西。

Promise/A+ spec mentions that then must return a promise and if onFulfilled is not a function, a then called on a resolved promise must return a new promise resolved with the value of the previous promise. It’s better to be expressed by code:

Promise / A +规范提到必须then返回一个promise并且如果onFulfilled 不是一个函数then调用了已解决的promise必须返回一个新的已解决的promise 该新的诺言以 先前的诺言 的值来解决 。 最好用代码表示:

let resolvedPromise = Promise.resolve("One"); // 1 resolvedPromise.then("two");                  // 2

Line 1 returns a promise resolved with “One”, but line 2 returns a new promise: Promise {<fulfilled>: “One”} resolved with “One” NOT “Two”. The string "Two" we pass the then() is ignored.

第1行返回以“One”解决的promise ,但第2行返回新promisePromise {<fulfilled>: “One”}“One”而非“Two” 。 我们通过then()传递的字符串"Two"将被忽略。

If we make a promise chain with several non-functions inserted for example:

如果我们创建一个承诺链,其中插入了几个非功能,例如:

resolvedPromise.then(func).then(non-func).then(func).then(non-func);

We can imagine that we strikethrough the .then(non-func) parts like:

我们可以想象我们删除了.then(non-func)部分,例如:

Some call this “promise fall through”. What if one of the non-func is a promise? You may think the promise chain won’t ignore a promise. Let’s try by code:

有人将此称为“承诺落空”。 如果non-func是一个promise怎么办? 您可能认为诺言链不会忽略promise 。 让我们尝试通过代码:

let resolvedPromise = Promise.resolve("I was resolved");let starterPromise = Promise.resolve("I am the starter promise.");starterPromise.then(resolvedPromise);

The last line returns Promise {<fulfilled>: “I am the starter promise.”} , the resolved value of the resolvedPromise we passed to then() was not taken. So there’s no exception for this rule. Function is the only currency within a promise chain. If you want to insert a promise into a promise chain, use a function that returns a promise, don’t do things like “first create a promise then insert it into the chain”.

最后一行返回Promise {<fulfilled>: “I am the starter promise.”} ,我们传递给then()resolvedPromise的解析值未获取。 因此,该规则也不例外。 功能是承诺链中的唯一货币 。 如果要将promise插入诺言链中,请使用返回promise的函数,不要做“先创建promise然后将其插入到链中”之类的事情。

2.5两种守信用 (2.5 two kinds of waiting on promises)

Personally I prefer to understand that there’re actually two kinds of waiting for a pending promise. One is wait from “outside”, the other is wait from “inside”.

我个人更喜欢了解实际上有很2种等待pending承诺。 一种是从“外部”等待,另一种是从“内部”等待。

“Wait from inside” means inside a Promise constructor, after a promise is created, it’s initially set to pending, and then it’s waiting to be either fulfilled or rejected. This kind of waiting is often neglected. On the contrary, the waiting made by then() is stressed a lot, and this is “wait from outside”. Both kinds of waitings wait on a promise to transit from pending to fulfilled/rejected, but they are different. Having a notion of this helped me better understand the states of promise as well as the behavior of a promise chain.

一个里面的意思是“从里等待” Promise构造,一个后promise被创建,它的初始设置为pending ,然后它等待或者履行或拒绝。 这种等待常常被忽略。 相反, then()进行的等待会受到很大的压力,这就是“从外部等待”。 这两种类型的waitings的对等 promise 过境从 pending fulfilled/rejected ,但它们是不同的。 了解这一点有助于我更好地了解承诺的状态以及承诺链的行为。

2.5.1 how to make a pending promise

2.5.1如何做出 pending 承诺

This is fun and easy. Remember I said in section 2.1 when trying to create a promise always consider Promise() constructor? So the answer of this is “just make it but don’t resolve it”. That is:

这既有趣又容易。 还记得我在一节说2.1试图创建一个时promise总是考虑Promise()构造函数? 因此,答案是“只是制造但不解决”。 那是:

let pendingPromise = new Promise((resolve, reject) => {}); 

By doing this we get a pending promise Promise {<pending>} since we don’t call resolve() or reject()at all inside the callback. Another theoretically possible scenario is we called resolve() or reject() but the time before that happens was “forever”. For example, resolve() or reject() is waiting to be called after a data retrieving task that never ends.

通过这样做,我们得到了一个待处理的Promise {<pending>}因为我们在回调内部根本不调用resolve()reject() 。 从理论上讲,另一种可能的情况是我们称为resolve()reject()但是发生这种情况之前的时间是“永远”。 例如,在数据检索任务永无止境之后, resolve()reject()正在等待被调用。

2.5.2 pauses on thens are visible

2.5.2暂停, then s可见

Now if we have a pending promise, let’s see how the chain will pause:

现在,如果我们有一个pending的承诺,让我们来看看链将如何暂停:

let pendingPromise = new Promise((resolve, reject) => {}); pendingPromise.then(() => console.log("Hello World.")); 
// ^
//paused

The console.log operation inside the callback we passed to then() didn’t get executed and the string “Hello World.” was not printed out. This is because pendingPromise is at the state of pending, the next then will wait on it. I often see words like “waiting on a promise”, though this is not wrong, but this gives us a sense that where there is a promise there is a waiting. But waiting only happens on pending promise.

我们传递给then()的回调内的console.log操作未执行,字符串“ Hello World”。 没有打印出来。 这是因为pendingPromise处于的状态pending ,接下来then等待就可以了。 我经常看到诸如“等待诺言”之类的词,尽管这没有错,但是这使我们感觉到,有诺言的地方就有等待的机会。 但是,只有等待发生的pending承诺。

2.5.3 then() only waits on pending promises doesn’t mean settled ones are skipped

2.5.3则()只在等待 pending 的承诺并不意味着解决那些被跳过

First look at a code example:

首先看一个代码示例:

Promise.resolve("one").then(() => Promise.resolve("two")); 
// Promise {<fulfilled>: "two"}Promise.resolve("one").then(() => Promise.resolve("two")).then(() => Promise.resolve("three"));
// Promise {<fulfilled>: "three"}

Here both lines start with a promise resolved with “one”. When we chain one then we get a new promise resolved with "two" . When chain two thens we get a new promise resolved with "three" . Based on line 1 we know there is a “middle promise” with "two" as its value existed transitorily. But no promise is skipped, even though they are resolved ones.

这两条线都以用“one”解决的promise开头。 当我们链中的一个then我们得到了一个新的promise以解决"two" 。 当链上的两个then š我们得到一个新的promise以解决"three" 。 根据第1行,我们知道存在一个带有"two"的“中间承诺”,因为其价值暂时存在。 但是,即使是已解决的promise ,也不会跳过任何promise

If we configure a promise chain appropriately, of course the chain will wait on pending promises, but the chain also won’t forget to go through every fulfilled or rejected ones.

如果我们适当地配置了一个Promise链,则该链当然会等待pending Promise,但是该链也不会忘记遍历所有已fulfilled或被rejected的诺言。

3尝试以标准方式培养亲密感 (3 Try to nurture intimacy with standard)

This is more of a suggestion than another key point, but I think it’s important for learning promise too.

这更多的是建议,而不是另一个关键点,但我认为这对于学习承诺也很重要。

If you’ve ever explored some articles about promise, you may have been introduced with the Promise/A+ standard, I mentioned it several times in this article. As it states, it’s:

如果您曾经浏览过一些有关Promise的文章,则可能是Promise / A +标准向您介绍的,我在本文中多次提到过。 声明为:

An open standard for sound, interoperable JavaScript promises — by implementers, for implementers.

一个开放的标准,用于实现可互操作JavaScript承诺-由实现者针对实现者。

In that page, there are just several sections of structured rules. So promise is more of a model, not some hard-coded packages. The rules describe how to implement promise, but there doesn’t exist a single right way to implement it. This is very similar to what we talk about the mental model of event loop. Actually if you have known the basic aspects of promise and are using the correct terms, reading the standard is more helpful and effective when you are confused by “promise puzzles”. The standard is really boring, but it’s also very reliable.

在该页面中,只有几部分结构化规则。 因此,promise只是一个模型,而不是一些硬编码的程序包。 规则描述了如何实现承诺,但是没有一个正确的方法来实现它。 这与我们谈论事件循环的心理模型非常相似。 实际上,如果您了解了诺言的基本方面并使用了正确的术语,那么当您对“诺言之谜”感到困惑时,阅读标准会更加有用和有效。 该标准确实很无聊,但也非常可靠。

4小结 (4 Summary)

In this 2-part article, I think the important points are:

在这篇分为两部分的文章中,我认为要点是:

  • the separation of async and sync is for better coordinating different tasks, and event loop model is one way to do the coordinating work.

    异步和同步的分离是为了更好地协调不同的任务,而事件循环模型是完成协调工作的一种方法。
  • differentiate sync and async part when using promise; there are two kinds of wait for a pending promise; function is the only currency in a promise chain.

    在使用promise时区分同步和异步部分; 有两种等待pending承诺的方法; 功能是承诺链中唯一的货币。

  • try to nurture intimacy with standards and docs.

    尝试与标准和文档建立亲密关系。

We’ve been through a long journey from setTimeout to Promise. In part 1, we spend most time discussing what is sync and async, and how they are coordinated by the event loop model. Although we barely mentioned promise in part 1 but all the discussion there will support our understanding of promise. In this part 2, I don’t write about how to use promise, instead I focus on some key points that may be missed during the process of learning promise. Hope this can help you a bit on the journey of exploring promise.

setTimeout到Promise,我们经历了漫长的旅程。 在第1部分中,我们花费大量时间讨论什么是同步和异步,以及事件循环模型如何协调它们。 尽管我们在第1部分中几乎没有提到诺言,但是那里的所有讨论将支持我们对诺言的理解。 在第2部分中,我不会写关于如何使用Promise的知识,而是将重点放在学习Promise过程中可能遗漏的一些关键点上。 希望这可以帮助您在探索承诺的过程中有所帮助。

翻译自: https://medium.com/@xullnn/on-exploring-promise-2-possible-overlooked-points-about-promise-9e232d632eb3

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值