The apply()
function in the async
module creates a callable function variable by applying some preset parameters to an existing function.
The following example demonstrates how this function can be used:
import apply from 'async/apply';// Defining a functionfunction favoriteAlbum(band, album){return "My favorite album is " + album + " by " + band}// Using apply to set parametervar ledZepAlbum = apply(favoriteAlbum, "Led Zeppelin")// Calling the resultant functionconsole.log(ledZepAlbum("Led Zeppelin IV"))
This example defines a function, favoriteAlbum
, and uses apply()
to apply a parameter to it. The resultant function, ledZepAlbum
, has its first parameter set to “Led Zeppelin.” The second parameter still needs to be in the function call (line 12 in this example) since it has not been set using the apply()
function.
The apply()
function, as seen on line 9, takes the function name as the first parameter and all the parameters that are to be applied to it as the following parameters.
Free Resources