How does function.apply.bind work in the following code?

后端 未结 4 1910
没有蜡笔的小新
没有蜡笔的小新 2020-12-24 15:58

So I get that an array of [200,599] is returned from the promise and the callback function inside spread is being passed into Function.apply.bind, but now I\'m lost. How is

4条回答
  •  无人及你
    2020-12-24 16:14

    The spread function is just a utility function to convert an array, into parameters passed to a function. The apply is doing the converting, and the bind is binding it to the calling function so that the "this" context is connected to same function.

    To see how spread is working in a simpler form ->

    spread(function (x,y){console.log(x,y);})([1,2])
    

    You will get the answer, 1 2, as 1 is passed to x, and 2 is passed to y.

    So in your example your promise.all is returning an array of resolved promises. These are then getting mapped to parameters to your function(x,y).

提交回复
热议问题