admin管理员组文章数量:1279148
So Promise.all passes an array as a value into the function, I would much rather it pass the array values as arguments.
Assume I have this function:
function printData(a,b,c){
console.log(a,b,c)
}
I would like
Promise.all([1,2,3]).then(printData)
>> [1,2,3] undefined undefined
To print this instead
>> 1 2 3
Is there a better way of doing this:
Promise.all([1,2,3,4]).then(function(values){printData.apply(null, values)})
using the spread operator?
I also tried
Promise.all([1,2,3]).then(printData.apply)
But it returns an error
So Promise.all passes an array as a value into the function, I would much rather it pass the array values as arguments.
Assume I have this function:
function printData(a,b,c){
console.log(a,b,c)
}
I would like
Promise.all([1,2,3]).then(printData)
>> [1,2,3] undefined undefined
To print this instead
>> 1 2 3
Is there a better way of doing this:
Promise.all([1,2,3,4]).then(function(values){printData.apply(null, values)})
using the spread operator?
I also tried
Promise.all([1,2,3]).then(printData.apply)
But it returns an error
Share Improve this question edited Feb 17, 2017 at 19:50 lonewarrior556 asked Feb 17, 2017 at 18:05 lonewarrior556lonewarrior556 4,5092 gold badges32 silver badges58 bronze badges4 Answers
Reset to default 9One way using ES 6 Destructuring
function printData(a,b,c){
console.log(a,b,c)
}
Promise.all([1,2,3]).then( data => {var [a,b,c] = data;
printData(a,b,c);});
Using ES 6 Spread Syntax
function printData(a,b,c){
console.log(a,b,c)
}
Promise.all([1,2,3]).then(data => printData(...data))
Instead of
.then(printData)
you can spread with
.then(args => printData(...args))
Trying to use the spread operator technically has you nesting functions, which works, but there is another way
Promise.all([1,2,3]).then(printData.apply)
Doesn't work because this is equal to:
printData.apply.call(undefined, [1,2,3])
which returns the same error
>>Uncaught TypeError: Function.prototype.apply was called on undefined,
which is a undefined and not a function
Promise
passes this
in to call
and it loses track of what it should be.
What you want is:
test.apply.call(test,[null,1,2,3])
which equals:
test.apply(null,[1,2,3])
which equals
test(1,2,3)
because you don't have control over Promise using call, use bind
to determine the arguments
printData.apply.bind(printData, null)
which when called equals
printData.apply.bind(printData, null).call(undefined, [1,2,3])
>> 1 2 3
so Finally:
Promise.all([1,2,3]).then(printData.apply.bind(printData,null))
>> 1 2 3
Here's a related question about bining apply and call Why can I not call a function.apply?
function printData(...a){
console.log(a.reduce((n,o)=>n.concat(o),[]).join(","));
}
Take all arguments, reduce all Arrays in Arrays to one Array, pass it as arguments to console.log.
http://jsbin./vutizahago/edit?console
本文标签: javascriptIs it possible to spread the input array into argumentsStack Overflow
版权声明:本文标题:javascript - Is it possible to spread the input array into arguments? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741261112a2367637.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论