Open In App

Node.js util.deprecate() Method

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, )

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 




// Node.js program to demonstrate the
// util.deprecate() method
 
// Import the util module
const util = require('util');
 
const deprecateFunction = util.deprecate(
    // Function which is deprecated
    function () { },
 
    // Warning message that is
    // printed to stderr
    "someWarningMessage",
 
    // Deprecated API
    'Deprecation API'
);
 
// Function call
deprecateFunction();

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 




// Node.js program to demonstrate the
// util.deprecate() method
 
// Import the util module
const util = require('util');
let outerValue = "along with"
 
const geekyFunction1 = util.deprecate(() => {
    console.log("This Deprecated function is working, ");
},
    // The arrow function which is deprecated
    "geekyFunction() is deprecated. Use "
    + "geeksForGeeksFunction() instead",
 
    // The warning message that is printed
    // to stderr
    'DEP0014' // Deprecated API
);
 
const geekyFunction2 = util.deprecate(function (call) {
    console.log("This Deprecated function is working, ",
        outerValue, call());
},
    // The function which is deprecated
    "geekyFunction() is deprecated. Use "
    + "geeksForGeeksFunction() instead",
    // The warning message that is printed to stderr
    'DEP0014' // Deprecated API
);
 
// Function call
geekyFunction1();
 
// Function call
geekyFunction2(function call() {
    console.log("Callback");
    return "return value"
});

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


Article Tags :