How to use the deprecate function from util

Find comprehensive JavaScript util.deprecate code examples handpicked from public code repositorys.

138
139
140
141
142
143
144
145
146
such a way that it is marked as deprecated.

```js
const util = require('util');

exports.obsoleteFunction = util.deprecate(() => {
  // Do something here.
}, 'obsoleteFunction() is deprecated. Use newShinyFunction() instead.');
```
fork icon0
star icon3
watch icon3

+ 5 other calls in file

212
213
214
215
216
217
218
219
220
221
222


function _alias(object, from, to) {
    try {
        let name = object.constructor.name
        Object.defineProperty(object, from, {
            value: util.deprecate(object[to], sub('%s.%s() is renamed to %s.%s()',
                name, from, name, to)),
            enumerable: false
        })
    } catch {}
fork icon0
star icon1
watch icon1

+ 15 other calls in file

58
59
60
61
62
63
64
65
66
67
68
69
70
 */


var fd = parseInt(process.env.DEBUG_FD, 10) || 2;


if (1 !== fd && 2 !== fd) {
  util.deprecate(function(){}, 'except for stderr(2) and stdout(1), any other usage of DEBUG_FD is deprecated. Override debug.log if you want to use a different log function (https://git.io/debug_fd)')()
}


var stream = 1 === fd ? process.stdout :
             2 === fd ? process.stderr :
fork icon0
star icon0
watch icon1

230
231
232
233
234
235
236
237
238
239
240
      throw new Error('Unknown state: ' + this.state)
    }
  }
}


Cursor.prototype.end = util.deprecate(
  Cursor.prototype.end,
  'Cursor.end is deprecated. Call end on the client itself to end a connection to the database.'
)

fork icon0
star icon0
watch icon0

2
3
4
5
6
7
8
9
10
11
12
const common = require('../common');
const assert = require('assert');
const util = require('util');


[1, true, false, null, {}].forEach((notString) => {
  assert.throws(() => util.deprecate(() => {}, 'message', notString), {
    code: 'ERR_INVALID_ARG_TYPE',
    name: 'TypeError',
    message: 'The "code" argument must be of type string.' +
             common.invalidArgTypeHelper(notString)
fork icon0
star icon0
watch icon0

-3
-2
-1
0
}, 'this function is deprecated');


deprecated();
fork icon0
star icon0
watch icon0

1
2
3
4
5
6
7
8
9
10
11
12
13
const assert = require('assert');


class deprecatedClass {
}


const deprecated = util.deprecate(deprecatedClass, 'deprecatedClass is deprecated.');


class subclass extends deprecated {
  constructor() {
    super();
fork icon0
star icon0
watch icon0