Try and understand this block from JavaScript you don’t know:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
if (!Promise.wrap) {
Promise.wrap = function(fn) {
return function() {
var args = [].slice.call( arguments ); // [].slice.call(arguments) creates a new copy of the array arguments.

return new Promise( function(resolve,reject){
fn.apply(
null,
args.concat( function(err,v){ // in addition to the arguments passed to the returned promisory, also create a callback function to the original callback-based fn. This callback is created to convert the callback invocation to Promise resolution.
if (err) {
reject( err );
}
else {
resolve( v );
}
} )
);
} );
};
};
}

fn.apply is a function call, fn.bind is creating a new function.