![]() |
VOOZH | about |
The util.deprecate() (Added in v0.8.0_ method is an inbuilt application programming interface of the util module which wraps fn (which may be a function or class) in such a way that it is marked as deprecated. When util.deprecate() method is called, it returns a function that emits a DeprecationWarning using the 'warning' event. The first time the returned function is called, the warning is emitted and printed to stderr. Once the warning is emitted, then the wrapped function is called. The warning is emitted only once if the same optional code is supplied in multiple calls to 'util.deprecate()'.
Syntax:
util.deprecate(fn, msg, [code][/code])
Parameters: This function accepts three parameters as mentioned above and described below:
Return Value: It returns the deprecated function wrapped to emit a warning, i.e. the warning message is returned along with deprecated APIs after running the complete function.
Example 1: In this example, we will see the use of the util.deprecate() method
Filename: index.js
Run the index.js file using the following command:
node index.js
Output:
[Deprecation API] DeprecationWarning: someWarningMessage
Example 2: In this example, we will see the use of the util.deprecate() method
Filename: index.js
Run the index.js file using the following command:
node index.js
Output:
This Deprecated function is working, Callback This Deprecated function is working, along with return value [DEP0014] DeprecationWarning: geekyFunction() is deprecated. Use geeksforgeeksFunction() instead
Conditions:
Reference: https://nodejs.org/api/util.html#util_util_deprecate_fn_msg_code