Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

apply
javascript
async

How to use the Apply function in JavaScript's Async Module

Educative Answers Team

Apply Function

The apply() function in the async module creates a callable function variable by applying some preset parameters to an existing function.

Usage

The following example demonstrates how this function can be used:

import apply from 'async/apply';

// Defining a function
function favoriteAlbum(band, album){
  return "My favorite album is " + album + " by " + band
}

// Using apply to set parameter
var ledZepAlbum = apply(favoriteAlbum, "Led Zeppelin")

// Calling the resultant function
console.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.

RELATED TAGS

apply
javascript
async
Copyright ©2022 Educative, Inc. All rights reserved
RELATED COURSES

View all Courses

Keep Exploring