Skip to content

Instantly share code, notes, and snippets.

Created February 23, 2017 15:31
What would you like to do?
exports.deprecate = function deprecate(fn, msg, code) {
// pre wrapper checks go here
function deprecated() {
if (!warned) {
warned = true;
if (code !== undefined) {
process.emitWarning(msg, 'DeprecationWarning', code, deprecated);
} else {
process.emitWarning(msg, 'DeprecationWarning', deprecated);
if ( {
return Reflect.construct(fn, arguments,;
return fn.apply(this, arguments);
// The wrapper will keep the same prototype as fn to maintain prototype chain
Object.setPrototypeOf(deprecated, fn);
if (fn.prototype) {
// Setting this (rather than using Object.setPrototype, as above) ensures
// that calling the unwrapped constructor gives an instanceof the wrapped
// constructor.
deprecated.prototype = fn.prototype;
return deprecated;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment