util.deprecate(fn, msg[, code])


util.deprecate() 方法以标记为已弃用的方式封装 fn(可能是函数或类)。

const util = require('util');

exports.obsoleteFunction = util.deprecate(() => {
  // 在这里做点事情。
}, 'obsoleteFunction() is deprecated. Use newShinyFunction() instead.');

当调用时,util.deprecate() 将返回将使用 'warning' 事件触发 DeprecationWarning 的函数。 第一次调用返回的函数时,警告将触发并打印到 stderr。 触发警告后,将调用封装的函数而不触发警告。

如果在多次调用 util.deprecate() 时提供了相同的可选 code,则该 code 只会触发一次警告。

const util = require('util');

const fn1 = util.deprecate(someFunction, someMessage, 'DEP0001');
const fn2 = util.deprecate(someOtherFunction, someOtherMessage, 'DEP0001');
fn1(); // 使用代码 DEP0001 触发弃用警告
fn2(); // 不触发弃用警告,因为它具有相同的代码
  • fn <Function> The function that is being deprecated.
  • msg <string> A warning message to display when the deprecated function is invoked.
  • code <string> A deprecation code. See the list of deprecated APIs for a list of codes.
  • Returns: <Function> The deprecated function wrapped to emit a warning.

The util.deprecate() method wraps fn (which may be a function or class) in such a way that it is marked as deprecated.

const util = require('util');

exports.obsoleteFunction = util.deprecate(() => {
  // Do something here.
}, 'obsoleteFunction() is deprecated. Use newShinyFunction() instead.');

When called, util.deprecate() will return a function that will emit a DeprecationWarning using the 'warning' event. The warning will be emitted and printed to stderr the first time the returned function is called. After the warning is emitted, the wrapped function is called without emitting a warning.

If the same optional code is supplied in multiple calls to util.deprecate(), the warning will be emitted only once for that code.

const util = require('util');

const fn1 = util.deprecate(someFunction, someMessage, 'DEP0001');
const fn2 = util.deprecate(someOtherFunction, someOtherMessage, 'DEP0001');
fn1(); // Emits a deprecation warning with code DEP0001
fn2(); // Does not emit a deprecation warning because it has the same code

If either the --no-deprecation or --no-warnings command line flags are used, or if the process.noDeprecation property is set to true prior to the first deprecation warning, the util.deprecate() method does nothing.

If the --trace-deprecation or --trace-warnings command line flags are set, or the process.traceDeprecation property is set to true, a warning and a stack trace are printed to stderr the first time the deprecated function is called.

If the --throw-deprecation command line flag is set, or the process.throwDeprecation property is set to true, then an exception will be thrown when the deprecated function is called.

The --throw-deprecation command line flag and process.throwDeprecation property take precedence over --trace-deprecation and process.traceDeprecation.