简体   繁体   English

Javascript,Promise.then返回值

[英]Javascript, Promise.then return values

Doing some research about Promises and I know that a Promise object can be in three states (pending, resolved, rejected). 做一些关于Promises研究,我知道Promise对象可以处于三种状态(待决,已解决,被拒绝)。 The logic responsible for each of these three states is in a callback function which gets passed into the Promise constructor. 负责这三种状态中的每一种的逻辑都在一个回调函数中,该函数被传递给Promise构造函数。 This callback function has 2 functions as its arguments resolve and reject which are called when this callback either results in success or failure. 此回调函数有2个函数作为其参数resolve和reject,当此回调导致成功或失败时,将调用这些函数。

After the Promise is instantiated we can then add response handler callbacks to the promise by calling the .then function on it. 在实例化Promise之后,我们可以通过调用它上面的.then函数将响应处理程序回调添加到promise。 The .then function takes 2 callback functions as its arguments. .then函数将2个回调函数作为其参数。 The first argument is the callback function is called in case the Promise resolve function is called and the second callback is called in case the Promise reject function is called. 第一个参数是在调用Promise resolve函数时调用回调函数,在调用Promise reject函数的情况下调用第二个回调函数。 You can also call .catch on Promises to handle the rejected promises although this is just syntactic sugar for: 您也可以在Promises上调用.catch来处理被拒绝的承诺,尽管这只是语法糖:

.then(undefined, () => { failure callback})

What I find harder to understand is the fact that the .then method returns a Promise . 我发现更难理解的是.then 方法返回一个 Promise For example in the following code: 例如,在以下代码中:

Example

 let random = (Math.random() * 10); let promise = new Promise((res, rej) => { if (random >= 5) { res(random); } rej(random); }); promise .then( (nr) => { console.log("succes: " + nr); return nr + 5; }) .then((nr) => { console.log(nr); }) .catch( (nr) => { console.log("failure: " + nr); }) 

Question: 题:

In the example at the first .then it returns : nr + 5 . 在第一个示例中.then然后返回: nr + 5 In resolve cases of the Promise this value is succesfully passed onto the second .then . 在解决Promise情况下,这个值成功传递到第二个.then How is this possible? 这怎么可能? Is it under the hood: 它是否在引擎盖下:

return new Promise((res,rej) => {
    res(nr + 5)
})

or is this caused by something else? 或者这是由别的东西引起的?

It is the behaviour of a promise, it is described here 这是承诺的行为,这里描述

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/then https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/then

In Return value section: 在返回值部分:

if the handler function returns a value, the promise returned by then gets resolved with the returned value as its value; 如果处理函数返回一个值,返回的承诺, then获取与返回的值作为其值解决;

Yes, this one of major features of promises : they are chainable. 是的,这是承诺主要特征之一 :它们是可链接的。

The then method does return a new promise that will be resolved with the result of the callback . then方法确实返回一个新的promise,它将使用回调的结果解析 It does indeed construct a new Promise and does call resolve with the return value when the callback will be called. 它确实构造了一个new Promise ,并在调用回调时使用返回值调用resolve

You might want to take a look at this toy implementation to see how it is implemented (without the error handling, though). 您可能想看看这个玩具实现 ,看看它是如何实现的(尽管没有错误处理)。

Look at this: Promises chaining 看看这个: 承诺链接

Normally, a value returned by a .then handler is immediately passed to the next handler. 通常,.then处理程序返回的值会立即传递给下一个处理程序。 But there's an exception. 但是有一个例外。

If the returned value is a promise, then the further execution is suspended until it settles. 如果返回的值是一个promise,那么进一步的执行将被暂停,直到它结束。 After that, the result of that promise is given to the next .then handler. 之后,该承诺的结果将被赋予下一个.then处理程序。

Basically, when you return a value, ie: (nr + 5) or 5 or [1, 2] or {a: 1} , Etc., that value is passed immediately to the next handler (.then). 基本上,当你返回一个值,即: (nr + 5) or 5 or [1, 2] or {a: 1}等时,该值立即传递给下一个处理程序(.then)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM