[英]Map an array of resolved promises to an array of values
I have an array of resolved Promises: 我有一系列已解决的Promises:
[Promise, Promise, Promise, ...]
Each of these promises when resolved returns a value. 解析后的每个承诺都会返回一个值。 How do I create a function that returns the map of this array of promises to the value that each promise resolves to?
如何创建一个函数,将这个promises数组的映射返回到每个promise所解析的值?
[value_1, value_2, value_3, ...]
Here's the closest I've gotten so far: 这是我到目前为止最接近的:
const mapPromisesToValues = promises => {
Promise.all(promises, values => {
// how do I return these values from mapPromisesToValues
return values
});
}
To be more specific, the reason why I ask is because I have a collection of arrays of Promises. 更具体地说,我问的原因是因为我有一组Promises数组。
const collection_1 = [Promise, Promise, Promise, ...];
const collection_2 = [Promise, Promise, Promise, ...];
//...
and I want to batch these collection of promises mapped into a collection of values into 1 object, that I will then pass into another function that will be called once - in this case React's setState
: 并且我想将这些映射到一个值集合中的Promise集合批处理为一个对象,然后我将传递给另一个将被调用一次的函数 - 在这种情况下React的
setState
:
this.setState({
...this.state,
values_1,
values_2,
//...
});
I have an array of resolved Promise ... how do I return these values from mapPromisesToValues
我有一个已解决的Promise数组...如何从mapPromisesToValues返回这些值
Even though you know the promises are resolved, you can't return a map of their values from your function. 即使您知道承诺已解决,也无法从函数中返回其值的映射。 The only way to get the value of a promise is via a
then
callback, and that callback is guaranteed to be called asynchronously, which means that your function cannot return the value; 获取promise的值的唯一方法是通过
then
回调,并且保证回调被异步调用,这意味着你的函数不能返回值; it can only return a promise of the value (eg, as Promise.all
does). 它只能返回值的承诺(例如,如
Promise.all
所做的那样)。
You'll have to just have your function return the Promise.all
and have the consumer of the function use it. 你必须让你的函数返回
Promise.all
并让函数的使用者使用它。
Re your edit: 重新编辑:
To be more specific, the reason why I ask is because I have a collection of arrays of Promises.
更具体地说,我问的原因是因为我有一组Promises数组。
const collection_1 = [Promise, Promise, Promise, ...]; const collection_2 = [Promise, Promise, Promise, ...];
//...
// ...
and I want to batch these collection of promises mapped into a collection of values into 1 object, that I will then pass into another function that will be called once - in this case React's
setState
:并且我想将这些映射到一个值集合中的Promise集合批处理为一个对象,然后我将传递给另一个将被调用一次的函数 - 在这种情况下React的
setState
:this.setState({ ...this.state, values_1, values_2, //... });
That's fine, you don't need to do that synchronously . 没关系,你不需要同步那样做。 Simply:
只是:
Promise.all([
Promise.all(collection_1),
Promise.all(collection_2),
// etc.
])
.then(([values_1, values_2, /*etc*/]) => {
this.setState({values_1, values_2,/*etc*/});
});
Note that I left off ...this.state
there. 请注意,我离开了
...this.state
那里。 Two reasons: 两个原因:
If you're setting state based on existing state, you must use the callback version of setState
, not the one accepting an object; 如果您基于现有状态设置状态,则必须使用
setState
的回调版本 ,而不是接受对象的回调版本 ; and 和
State updates can be partial, so ...this.state
is unnecessary extra work. 状态更新可能是部分的,所以
...this.state
是不必要的额外工作。
Depending on your coding environment, you may find it useful to adopt async
/ await
(for instance, if you're transpiling, or you're doing this on Node.js can can use an up-to-date copy of Node.js). 根据您的编码环境,您可能会发现采用
async
/ await
很有用(例如,如果您正在进行转换,或者您在Node.js上执行此操作,则可以使用Node.js的最新副本)。 async
/ await
lets you write your code according to its logical flow , rather than its synchronous flow. async
/ await
允许您根据其逻辑流而不是其同步流来编写代码。 Within an async
function, you use await
to suspend the function and wait for a promise to resolve. 在
async
函数中,您使用await
挂起函数并等待承诺解析。 async
functions are syntactic sugar for functions that return promises, and await
is syntactic sugar for consuming promises (eg, then
and catch
). async
函数是返回promises的函数的语法糖,而await
是用于消费promises的语法糖(例如, then
和catch
)。
To make the transition from synchronous-flow to logical-flow, you make your entry point an async
function that you handle errors from: 要使从同步流转换为逻辑流,可以使入口点成为
async
函数,以便处理以下错误:
(async () => {
// Your logical flow code here
}).catch(error => {
// Handle the error
});
Then, your function could be an async
function: 然后,您的函数可能是
async
函数:
async function example() {
// ...
return await Promise.all(thePromises)
}
...which any other async
function can use via await
: ...任何其他
async
函数可以通过await
使用:
let values = await example();
You can't get the values from the promises synchronously by the nature of promises, sadly. 遗憾的是,你无法通过承诺的性质同步地从承诺中获得价值。 You have to do this:
你必须这样做:
const mapPromisesToValues = promises => {
return Promise.all(promises);
}
mapPromisesToValues(promises).then(values => console.log(values));
const values = await Promise.all(promiseArr);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.