【发布时间】:2017-04-22 17:09:08
【问题描述】:
function getMyFunction(data) {
return () => new Promise((resolve, reject) => {
resolve('here is the value:' + data);
});
}
const whatToGet = [
'a',
'b',
'c',
'd',
'e',
];
const stuffArray = whatToGet.map(thing => getMyFunction(thing));
Promise.all(stuffArray).then((result) => {
console.log('result: ', result);
});
期待
result: [
'here is the value: a',
'here is the value: b',
'here is the value: c',
'here is the value: d',
'here is the value: e'
]
但我却得到了结果:
result: [ () => new Promise((resolve, reject) => {
resolve('here is the value:', data);
}), () => new Promise((resolve, reject) => {
resolve('here is the value:', data);
}), () => new Promise((resolve, reject) => {
resolve('here is the value:', data);
}), () => new Promise((resolve, reject) => {
resolve('here is the value:', data);
}), () => new Promise((resolve, reject) => {
resolve('here is the value:', data);
})
]
【问题讨论】:
-
你只是问了基本相同的问题并得到了适当的答案
-
@charlietfl 完全不同的问题。
-
不,不是……基本原理是完全一样的,只是不是传递一个函数,而是返回一个函数数组……所有这些都需要被调用,或者其他人建议只返回承诺
-
老实说,这个功能似乎是多余的......虽然如果你在上一条评论中准确地询问了你提出的问题......并链接到其他问题,也许不会被认为是同一个问题。或者只是向原始回答者寻求帮助
标签: javascript