Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

util.deprecate does not work as expected #15761

Closed
hiiwave opened this issue Oct 3, 2017 · 2 comments
Closed

util.deprecate does not work as expected #15761

hiiwave opened this issue Oct 3, 2017 · 2 comments

Comments

@hiiwave
Copy link

hiiwave commented Oct 3, 2017

According to the document, by calling util.deprecate(function, string),
it should run the function before printing the string on stderr.
However, I find that the function does NOT run when I call util.deprecate.

To reproduce it, use the sample code in the document

// test.js
const util = require('util');

exports.puts = util.deprecate(function() {
  for (let i = 0, len = arguments.length; i < len; ++i) {
    process.stdout.write(arguments[i] + '\n');
  }
}, 'util.puts: Use console.log instead');

And the output is nothing:

$ node test.js
$ 

Another example is as follows:

const util = require('util');
console.log(util.deprecate);
var f = function() {
    console.log("F");
};
util.deprecate(f, "DEPRECATED");
console.log("END");

And the output does not show F as expected:

$ node test.js
[Function: deprecate]
END
$ 

Do I have any misunderstanding or is there any tricky issue upon this function? Thanks a lot.

@vsemozhetbyt
Copy link
Contributor

util.deprecate() returns the function wrapped. You need to call the returned function to get the warning:

const util = require('util');

const f = util.deprecate(function() { console.log("F"); }, "DEPRECATED");

f();
F
(node:1176) DeprecationWarning: DEPRECATED

@hiiwave
Copy link
Author

hiiwave commented Oct 3, 2017

Oh, that's my carelessness. Thank you very much for your help.

@hiiwave hiiwave closed this as completed Oct 3, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants