Lodash _.defer() Method
Lodash _.defer() method in lodash is used to defer the calling of the func parameter until the recent call stack is cleared. Moreover, any further arguments are provided to the function parameter of this method when it is called.
Syntax:
_.defer(func, [args]);
Parameters:
- func: It is the function that is to be deferred.
- [args]: It is the arguments with which the function is being called.
Return Value:
This method returns the timer id.
Example 1: In this example, we are using the lodash _.defer() method because of this ‘Content’ is printing first.
Javascript
// Requiring lodash library const _ = require( 'lodash' ); // Calling defer() method with // its parameter _.defer( function (content) { console.log(content); }, 'w3wiki!' ); // Prints content after this console.log( 'Content:' ); |
Output:
Content:
w3wiki!
Example 2: In this example, we are using the lodash _.defer() method because of this ‘Integers are as follows:’ is printing first.
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
Contact Us