Lodash is a JavaScript library that works on the top of underscore.js. Lodash helps in working with arrays, strings, objects, numbers, etc.
The _.defer() method in lodash is used to defer the calling of func parameter until the recent call stack is cleared. Moreover, any further arguments are provided to func parameter of this method when it is called.
Syntax:
_.defer(func, [args])
Parameters: This method accepts two parameters which are described below:
- func: It is the function which is to be deferred.
- [args]: It is the arguments with which the func is being called.
Return Value: This method returns the timer id.
Example 1:
Javascript
// Requiring lodash library const _ = require( 'lodash' ); // Calling defer() method with // its parameter _.defer( function (content) { console.log(content); }, 'neveropen!' ); // Prints content after this console.log( 'Content:' ); |
Output:
Content: neveropen!
Example 2:
Javascript
// Requiring lodash library const _ = require( 'lodash' ); // Defining func parameter let func = number => { console.log(number); }; // Defining for loop for (let i = 1; i <= 5; i++) { // Calling defer() method // with its parameter _.defer(func, i); } // Prints integer after this console.log( 'Integers are as follows:' ); |
Output:
Integers are as follows: 1 2 3 4 5
Reference: https://lodash.com/docs/4.17.15#defer