有没有办法为Promisee.all中的每个结果分配名称?
我们说这是我的代码:
Promise.all([getBalls, getKids, getTeams])
.then(function (results) {
const new_team = doSomething(results[0], results[1],results[2])
resolve(new_team);
});
我希望它像:
Promise.all([
balls: getBalls,
kids: getKids,
teams: getTeams
]).then(function (results) {
const new_team = doSomething(balls,kids,teams)
resolve(new_team);
});
答案 0 :(得分:3)
您可以在function isValid(value) {
return value != undefined;
}
var arr = [12, undefined, "blabla", ,true, 44];
var filtered = arr.filter(isValid);
console.log(filtered); // [12, "blabla", true, 44]
回调函数中使用ES2015的Array destructuring:
.then
答案 1 :(得分:0)
在最近的node.js(> node v6.x
)版本中,您可以使用解构并执行
Promise.all([getBalls, getKids, getTeams])
.then(function ([balls, kids, teams]) {
const new_team = doSomething(results[0], results[1],results[2])
resolve(new_team);
});
答案 2 :(得分:0)
也许Promise.props看起来更容易使用。 http://bluebirdjs.com/docs/api/promise.props.html
Promise.props({
balls: getBalls,
kids: getKids,
teams: getTeams
})
.then((results) => {
const new_team = doSomething(results.balls, results.kids, results.teams)
resolve(new_team);
});