-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathutil.html
More file actions
2124 lines (1865 loc) · 160 KB
/
util.html
File metadata and controls
2124 lines (1865 loc) · 160 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>Util | Node.js v12.0.0 Documentation</title>
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Lato:400,700,400italic">
<link rel="stylesheet" href="assets/style.css">
<link rel="stylesheet" href="assets/hljs.css">
<link rel="canonical" href="https://nodejs.org/api/util.html">
</head>
<body class="alt apidoc" id="api-section-util">
<div id="content" class="clearfix">
<div id="column2" class="interior">
<div id="intro" class="interior">
<a href="/" title="Go back to the home page">
Node.js
</a>
</div>
<ul>
<li><a href="documentation.html" class="nav-documentation">About these Docs</a></li>
<li><a href="synopsis.html" class="nav-synopsis">Usage & Example</a></li>
</ul>
<div class="line"></div>
<ul>
<li><a href="assert.html" class="nav-assert">Assertion Testing</a></li>
<li><a href="async_hooks.html" class="nav-async_hooks">Async Hooks</a></li>
<li><a href="buffer.html" class="nav-buffer">Buffer</a></li>
<li><a href="addons.html" class="nav-addons">C++ Addons</a></li>
<li><a href="n-api.html" class="nav-n-api">C/C++ Addons - N-API</a></li>
<li><a href="child_process.html" class="nav-child_process">Child Processes</a></li>
<li><a href="cluster.html" class="nav-cluster">Cluster</a></li>
<li><a href="cli.html" class="nav-cli">Command Line Options</a></li>
<li><a href="console.html" class="nav-console">Console</a></li>
<li><a href="crypto.html" class="nav-crypto">Crypto</a></li>
<li><a href="debugger.html" class="nav-debugger">Debugger</a></li>
<li><a href="deprecations.html" class="nav-deprecations">Deprecated APIs</a></li>
<li><a href="dns.html" class="nav-dns">DNS</a></li>
<li><a href="domain.html" class="nav-domain">Domain</a></li>
<li><a href="esm.html" class="nav-esm">ECMAScript Modules</a></li>
<li><a href="errors.html" class="nav-errors">Errors</a></li>
<li><a href="events.html" class="nav-events">Events</a></li>
<li><a href="fs.html" class="nav-fs">File System</a></li>
<li><a href="globals.html" class="nav-globals">Globals</a></li>
<li><a href="http.html" class="nav-http">HTTP</a></li>
<li><a href="http2.html" class="nav-http2">HTTP/2</a></li>
<li><a href="https.html" class="nav-https">HTTPS</a></li>
<li><a href="inspector.html" class="nav-inspector">Inspector</a></li>
<li><a href="intl.html" class="nav-intl">Internationalization</a></li>
<li><a href="modules.html" class="nav-modules">Modules</a></li>
<li><a href="net.html" class="nav-net">Net</a></li>
<li><a href="os.html" class="nav-os">OS</a></li>
<li><a href="path.html" class="nav-path">Path</a></li>
<li><a href="perf_hooks.html" class="nav-perf_hooks">Performance Hooks</a></li>
<li><a href="process.html" class="nav-process">Process</a></li>
<li><a href="punycode.html" class="nav-punycode">Punycode</a></li>
<li><a href="querystring.html" class="nav-querystring">Query Strings</a></li>
<li><a href="readline.html" class="nav-readline">Readline</a></li>
<li><a href="repl.html" class="nav-repl">REPL</a></li>
<li><a href="stream.html" class="nav-stream">Stream</a></li>
<li><a href="string_decoder.html" class="nav-string_decoder">String Decoder</a></li>
<li><a href="timers.html" class="nav-timers">Timers</a></li>
<li><a href="tls.html" class="nav-tls">TLS/SSL</a></li>
<li><a href="tracing.html" class="nav-tracing">Trace Events</a></li>
<li><a href="tty.html" class="nav-tty">TTY</a></li>
<li><a href="dgram.html" class="nav-dgram">UDP/Datagram</a></li>
<li><a href="url.html" class="nav-url">URL</a></li>
<li><a href="util.html" class="nav-util active">Utilities</a></li>
<li><a href="v8.html" class="nav-v8">V8</a></li>
<li><a href="vm.html" class="nav-vm">VM</a></li>
<li><a href="worker_threads.html" class="nav-worker_threads">Worker Threads</a></li>
<li><a href="zlib.html" class="nav-zlib">ZLIB</a></li>
</ul>
<div class="line"></div>
<ul>
<li><a href="https://github.com/nodejs/node" class="nav-https-github-com-nodejs-node">GitHub Repo & Issue Tracker</a></li>
</ul>
</div>
<div id="column1" data-id="util" class="interior">
<header>
<h1>Node.js v12.0.0 Documentation</h1>
<div id="gtoc">
<ul>
<li>
<a href="index.html" name="toc">Index</a>
</li>
<li>
<a href="all.html">View on single page</a>
</li>
<li>
<a href="util.json">View as JSON</a>
</li>
<li class="version-picker">
<a href="#">View another version <span>▼</span></a>
<ol class="version-picker"><li><a href="https://nodejs.org/docs/latest-v11.x/api/util.html">11.x</a></li>
<li><a href="https://nodejs.org/docs/latest-v10.x/api/util.html">10.x <b>LTS</b></a></li>
<li><a href="https://nodejs.org/docs/latest-v9.x/api/util.html">9.x</a></li>
<li><a href="https://nodejs.org/docs/latest-v8.x/api/util.html">8.x <b>LTS</b></a></li>
<li><a href="https://nodejs.org/docs/latest-v7.x/api/util.html">7.x</a></li>
<li><a href="https://nodejs.org/docs/latest-v6.x/api/util.html">6.x <b>LTS</b></a></li>
<li><a href="https://nodejs.org/docs/latest-v5.x/api/util.html">5.x</a></li>
<li><a href="https://nodejs.org/docs/latest-v4.x/api/util.html">4.x</a></li>
<li><a href="https://nodejs.org/docs/latest-v0.12.x/api/util.html">0.12.x</a></li>
<li><a href="https://nodejs.org/docs/latest-v0.10.x/api/util.html">0.10.x</a></li></ol>
</li>
<li class="edit_on_github"><a href="https://github.com/nodejs/node/edit/master/doc/api/util.md"><span class="github_icon"><svg height="16" width="16" viewBox="0 0 16.1 16.1" fill="currentColor"><path d="M8 0a8 8 0 0 0-2.5 15.6c.4 0 .5-.2.5-.4v-1.5c-2 .4-2.5-.5-2.7-1 0-.1-.5-.9-.8-1-.3-.2-.7-.6 0-.6.6 0 1 .6 1.2.8.7 1.2 1.9 1 2.4.7 0-.5.2-.9.5-1-1.8-.3-3.7-1-3.7-4 0-.9.3-1.6.8-2.2 0-.2-.3-1 .1-2 0 0 .7-.3 2.2.7a7.4 7.4 0 0 1 4 0c1.5-1 2.2-.8 2.2-.8.5 1.1.2 2 .1 2.1.5.6.8 1.3.8 2.2 0 3-1.9 3.7-3.6 4 .3.2.5.7.5 1.4v2.2c0 .2.1.5.5.4A8 8 0 0 0 16 8a8 8 0 0 0-8-8z"/></svg></span>Edit on GitHub</a></li>
</ul>
</div>
<hr>
</header>
<div id="toc">
<h2>Table of Contents</h2>
<ul>
<li>
<p><span class="stability_2"><a href="#util_util">Util</a></span></p>
<ul>
<li><a href="#util_util_callbackify_original">util.callbackify(original)</a></li>
<li><a href="#util_util_debuglog_section">util.debuglog(section)</a></li>
<li><a href="#util_util_deprecate_fn_msg_code">util.deprecate(fn, msg[, code])</a></li>
<li><a href="#util_util_format_format_args">util.format(format[, ...args])</a></li>
<li><a href="#util_util_formatwithoptions_inspectoptions_format_args">util.formatWithOptions(inspectOptions, format[, ...args])</a></li>
<li><a href="#util_util_getsystemerrorname_err">util.getSystemErrorName(err)</a></li>
<li><a href="#util_util_inherits_constructor_superconstructor">util.inherits(constructor, superConstructor)</a></li>
<li><a href="#util_util_inspect_object_options">util.inspect(object[, options])</a></li>
<li>
<p><a href="#util_util_inspect_object_showhidden_depth_colors">util.inspect(object[, showHidden[, depth[, colors]]])</a></p>
<ul>
<li><a href="#util_customizing_util_inspect_colors">Customizing <code>util.inspect</code> colors</a></li>
<li><a href="#util_custom_inspection_functions_on_objects">Custom inspection functions on Objects</a></li>
<li><a href="#util_util_inspect_custom">util.inspect.custom</a></li>
<li><a href="#util_util_inspect_defaultoptions">util.inspect.defaultOptions</a></li>
</ul>
</li>
<li><a href="#util_util_isdeepstrictequal_val1_val2">util.isDeepStrictEqual(val1, val2)</a></li>
<li>
<p><a href="#util_util_promisify_original">util.promisify(original)</a></p>
<ul>
<li><a href="#util_custom_promisified_functions">Custom promisified functions</a></li>
<li><a href="#util_util_promisify_custom">util.promisify.custom</a></li>
</ul>
</li>
<li>
<p><a href="#util_class_util_textdecoder">Class: util.TextDecoder</a></p>
<ul>
<li>
<p><a href="#util_whatwg_supported_encodings">WHATWG Supported Encodings</a></p>
<ul>
<li><a href="#util_encodings_supported_without_icu">Encodings Supported Without ICU</a></li>
<li><a href="#util_encodings_supported_by_default_with_icu">Encodings Supported by Default (With ICU)</a></li>
<li><a href="#util_encodings_requiring_full_icu_data">Encodings Requiring Full ICU Data</a></li>
</ul>
</li>
<li><a href="#util_new_textdecoder_encoding_options">new TextDecoder([encoding[, options]])</a></li>
<li><a href="#util_textdecoder_decode_input_options">textDecoder.decode([input[, options]])</a></li>
<li><a href="#util_textdecoder_encoding">textDecoder.encoding</a></li>
<li><a href="#util_textdecoder_fatal">textDecoder.fatal</a></li>
<li><a href="#util_textdecoder_ignorebom">textDecoder.ignoreBOM</a></li>
</ul>
</li>
<li>
<p><a href="#util_class_util_textencoder">Class: util.TextEncoder</a></p>
<ul>
<li><a href="#util_textencoder_encode_input">textEncoder.encode([input])</a></li>
<li><a href="#util_textencoder_encoding">textEncoder.encoding</a></li>
</ul>
</li>
<li>
<p><a href="#util_util_types">util.types</a></p>
<ul>
<li><a href="#util_util_types_isanyarraybuffer_value">util.types.isAnyArrayBuffer(value)</a></li>
<li><a href="#util_util_types_isargumentsobject_value">util.types.isArgumentsObject(value)</a></li>
<li><a href="#util_util_types_isarraybuffer_value">util.types.isArrayBuffer(value)</a></li>
<li><a href="#util_util_types_isasyncfunction_value">util.types.isAsyncFunction(value)</a></li>
<li><a href="#util_util_types_isbigint64array_value">util.types.isBigInt64Array(value)</a></li>
<li><a href="#util_util_types_isbiguint64array_value">util.types.isBigUint64Array(value)</a></li>
<li><a href="#util_util_types_isbooleanobject_value">util.types.isBooleanObject(value)</a></li>
<li><a href="#util_util_types_isboxedprimitive_value">util.types.isBoxedPrimitive(value)</a></li>
<li><a href="#util_util_types_isdataview_value">util.types.isDataView(value)</a></li>
<li><a href="#util_util_types_isdate_value">util.types.isDate(value)</a></li>
<li><a href="#util_util_types_isexternal_value">util.types.isExternal(value)</a></li>
<li><a href="#util_util_types_isfloat32array_value">util.types.isFloat32Array(value)</a></li>
<li><a href="#util_util_types_isfloat64array_value">util.types.isFloat64Array(value)</a></li>
<li><a href="#util_util_types_isgeneratorfunction_value">util.types.isGeneratorFunction(value)</a></li>
<li><a href="#util_util_types_isgeneratorobject_value">util.types.isGeneratorObject(value)</a></li>
<li><a href="#util_util_types_isint8array_value">util.types.isInt8Array(value)</a></li>
<li><a href="#util_util_types_isint16array_value">util.types.isInt16Array(value)</a></li>
<li><a href="#util_util_types_isint32array_value">util.types.isInt32Array(value)</a></li>
<li><a href="#util_util_types_ismap_value">util.types.isMap(value)</a></li>
<li><a href="#util_util_types_ismapiterator_value">util.types.isMapIterator(value)</a></li>
<li><a href="#util_util_types_ismodulenamespaceobject_value">util.types.isModuleNamespaceObject(value)</a></li>
<li><a href="#util_util_types_isnativeerror_value">util.types.isNativeError(value)</a></li>
<li><a href="#util_util_types_isnumberobject_value">util.types.isNumberObject(value)</a></li>
<li><a href="#util_util_types_ispromise_value">util.types.isPromise(value)</a></li>
<li><a href="#util_util_types_isproxy_value">util.types.isProxy(value)</a></li>
<li><a href="#util_util_types_isregexp_value">util.types.isRegExp(value)</a></li>
<li><a href="#util_util_types_isset_value">util.types.isSet(value)</a></li>
<li><a href="#util_util_types_issetiterator_value">util.types.isSetIterator(value)</a></li>
<li><a href="#util_util_types_issharedarraybuffer_value">util.types.isSharedArrayBuffer(value)</a></li>
<li><a href="#util_util_types_isstringobject_value">util.types.isStringObject(value)</a></li>
<li><a href="#util_util_types_issymbolobject_value">util.types.isSymbolObject(value)</a></li>
<li><a href="#util_util_types_istypedarray_value">util.types.isTypedArray(value)</a></li>
<li><a href="#util_util_types_isuint8array_value">util.types.isUint8Array(value)</a></li>
<li><a href="#util_util_types_isuint8clampedarray_value">util.types.isUint8ClampedArray(value)</a></li>
<li><a href="#util_util_types_isuint16array_value">util.types.isUint16Array(value)</a></li>
<li><a href="#util_util_types_isuint32array_value">util.types.isUint32Array(value)</a></li>
<li><a href="#util_util_types_isweakmap_value">util.types.isWeakMap(value)</a></li>
<li><a href="#util_util_types_isweakset_value">util.types.isWeakSet(value)</a></li>
<li><a href="#util_util_types_iswebassemblycompiledmodule_value">util.types.isWebAssemblyCompiledModule(value)</a></li>
</ul>
</li>
<li>
<p><a href="#util_deprecated_apis">Deprecated APIs</a></p>
<ul>
<li><span class="stability_0"><a href="#util_util_extend_target_source">util._extend(target, source)</a></span></li>
<li><span class="stability_0"><a href="#util_util_debug_string">util.debug(string)</a></span></li>
<li><span class="stability_0"><a href="#util_util_error_strings">util.error([...strings])</a></span></li>
<li><span class="stability_0"><a href="#util_util_isarray_object">util.isArray(object)</a></span></li>
<li><span class="stability_0"><a href="#util_util_isboolean_object">util.isBoolean(object)</a></span></li>
<li><span class="stability_0"><a href="#util_util_isbuffer_object">util.isBuffer(object)</a></span></li>
<li><span class="stability_0"><a href="#util_util_isdate_object">util.isDate(object)</a></span></li>
<li><span class="stability_0"><a href="#util_util_iserror_object">util.isError(object)</a></span></li>
<li><span class="stability_0"><a href="#util_util_isfunction_object">util.isFunction(object)</a></span></li>
<li><span class="stability_0"><a href="#util_util_isnull_object">util.isNull(object)</a></span></li>
<li><span class="stability_0"><a href="#util_util_isnullorundefined_object">util.isNullOrUndefined(object)</a></span></li>
<li><span class="stability_0"><a href="#util_util_isnumber_object">util.isNumber(object)</a></span></li>
<li><span class="stability_0"><a href="#util_util_isobject_object">util.isObject(object)</a></span></li>
<li><span class="stability_0"><a href="#util_util_isprimitive_object">util.isPrimitive(object)</a></span></li>
<li><span class="stability_0"><a href="#util_util_isregexp_object">util.isRegExp(object)</a></span></li>
<li><span class="stability_0"><a href="#util_util_isstring_object">util.isString(object)</a></span></li>
<li><span class="stability_0"><a href="#util_util_issymbol_object">util.isSymbol(object)</a></span></li>
<li><span class="stability_0"><a href="#util_util_isundefined_object">util.isUndefined(object)</a></span></li>
<li><span class="stability_0"><a href="#util_util_log_string">util.log(string)</a></span></li>
<li><span class="stability_0"><a href="#util_util_print_strings">util.print([...strings])</a></span></li>
<li><span class="stability_0"><a href="#util_util_puts_strings">util.puts([...strings])</a></span></li>
</ul>
</li>
</ul>
</li>
</ul>
</div>
<div id="apicontent">
<h1>Util<span><a class="mark" href="#util_util" id="util_util">#</a></span></h1>
<p></p><div class="api_stability api_stability_2"><a href="documentation.html#documentation_stability_index">Stability: 2</a> - Stable</div><p></p>
<p>The <code>util</code> module is primarily designed to support the needs of Node.js' own
internal APIs. However, many of the utilities are useful for application and
module developers as well. It can be accessed using:</p>
<pre><code class="language-js"><span class="hljs-keyword">const</span> util = <span class="hljs-built_in">require</span>(<span class="hljs-string">'util'</span>);</code></pre>
<h2>util.callbackify(original)<a class="srclink" href="https://github.com/runkitdev/node/blob/cf1ce2bcc1a88ef6ff1bf745a19913a69607fb02/lib/util.js#L370">[src]</a><span><a class="mark" href="#util_util_callbackify_original" id="util_util_callbackify_original">#</a></span></h2>
<div class="api_metadata">
<span>Added in: v8.2.0</span>
</div>
<ul>
<li><code>original</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> An <code>async</code> function</li>
<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> a callback style function</li>
</ul>
<p>Takes an <code>async</code> function (or a function that returns a <code>Promise</code>) and returns a
function following the error-first callback style, i.e. taking
an <code>(err, value) => ...</code> callback as the last argument. In the callback, the
first argument will be the rejection reason (or <code>null</code> if the <code>Promise</code>
resolved), and the second argument will be the resolved value.</p>
<pre><code class="language-js"><span class="hljs-keyword">const</span> util = <span class="hljs-built_in">require</span>(<span class="hljs-string">'util'</span>);
<span class="hljs-keyword">async</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">fn</span>(<span class="hljs-params"></span>) </span>{
<span class="hljs-keyword">return</span> <span class="hljs-string">'hello world'</span>;
}
<span class="hljs-keyword">const</span> callbackFunction = util.callbackify(fn);
callbackFunction(<span class="hljs-function">(<span class="hljs-params">err, ret</span>) =></span> {
<span class="hljs-keyword">if</span> (err) <span class="hljs-keyword">throw</span> err;
<span class="hljs-built_in">console</span>.log(ret);
});</code></pre>
<p>Will print:</p>
<pre><code class="language-txt">hello world</code></pre>
<p>The callback is executed asynchronously, and will have a limited stack trace.
If the callback throws, the process will emit an <a href="process.html#process_event_uncaughtexception"><code>'uncaughtException'</code></a>
event, and if not handled will exit.</p>
<p>Since <code>null</code> has a special meaning as the first argument to a callback, if a
wrapped function rejects a <code>Promise</code> with a falsy value as a reason, the value
is wrapped in an <code>Error</code> with the original value stored in a field named
<code>reason</code>.</p>
<pre><code class="language-js"><span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">fn</span>(<span class="hljs-params"></span>) </span>{
<span class="hljs-keyword">return</span> <span class="hljs-built_in">Promise</span>.reject(<span class="hljs-literal">null</span>);
}
<span class="hljs-keyword">const</span> callbackFunction = util.callbackify(fn);
callbackFunction(<span class="hljs-function">(<span class="hljs-params">err, ret</span>) =></span> {
<span class="hljs-comment">// When the Promise was rejected with `null` it is wrapped with an Error and</span>
<span class="hljs-comment">// the original value is stored in `reason`.</span>
err && err.hasOwnProperty(<span class="hljs-string">'reason'</span>) && err.reason === <span class="hljs-literal">null</span>; <span class="hljs-comment">// true</span>
});</code></pre>
<h2>util.debuglog(section)<a class="srclink" href="https://github.com/runkitdev/node/blob/cf1ce2bcc1a88ef6ff1bf745a19913a69607fb02/lib/util.js#L206">[src]</a><span><a class="mark" href="#util_util_debuglog_section" id="util_util_debuglog_section">#</a></span></h2>
<div class="api_metadata">
<span>Added in: v0.11.3</span>
</div>
<ul>
<li><code>section</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> A string identifying the portion of the application for
which the <code>debuglog</code> function is being created.</li>
<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> The logging function</li>
</ul>
<p>The <code>util.debuglog()</code> method is used to create a function that conditionally
writes debug messages to <code>stderr</code> based on the existence of the <code>NODE_DEBUG</code>
environment variable. If the <code>section</code> name appears within the value of that
environment variable, then the returned function operates similar to
<a href="console.html#console_console_error_data_args"><code>console.error()</code></a>. If not, then the returned function is a no-op.</p>
<pre><code class="language-js"><span class="hljs-keyword">const</span> util = <span class="hljs-built_in">require</span>(<span class="hljs-string">'util'</span>);
<span class="hljs-keyword">const</span> debuglog = util.debuglog(<span class="hljs-string">'foo'</span>);
debuglog(<span class="hljs-string">'hello from foo [%d]'</span>, <span class="hljs-number">123</span>);</code></pre>
<p>If this program is run with <code>NODE_DEBUG=foo</code> in the environment, then
it will output something like:</p>
<pre><code class="language-txt">FOO 3245: hello from foo [123]</code></pre>
<p>where <code>3245</code> is the process id. If it is not run with that
environment variable set, then it will not print anything.</p>
<p>The <code>section</code> supports wildcard also:</p>
<pre><code class="language-js"><span class="hljs-keyword">const</span> util = <span class="hljs-built_in">require</span>(<span class="hljs-string">'util'</span>);
<span class="hljs-keyword">const</span> debuglog = util.debuglog(<span class="hljs-string">'foo-bar'</span>);
debuglog(<span class="hljs-string">'hi there, it\'s foo-bar [%d]'</span>, <span class="hljs-number">2333</span>);</code></pre>
<p>if it is run with <code>NODE_DEBUG=foo*</code> in the environment, then it will output
something like:</p>
<pre><code class="language-txt">FOO-BAR 3257: hi there, it's foo-bar [2333]</code></pre>
<p>Multiple comma-separated <code>section</code> names may be specified in the <code>NODE_DEBUG</code>
environment variable: <code>NODE_DEBUG=fs,net,tls</code>.</p>
<h2>util.deprecate(fn, msg[, code])<span><a class="mark" href="#util_util_deprecate_fn_msg_code" id="util_util_deprecate_fn_msg_code">#</a></span></h2>
<div class="api_metadata">
<details class="changelog"><summary>History</summary>
<table>
<tbody><tr><th>Version</th><th>Changes</th></tr>
<tr><td>v10.0.0</td>
<td><p>Deprecation warnings are only emitted once for each code.</p></td></tr>
<tr><td>v0.8.0</td>
<td><p><span>Added in: v0.8.0</span></p></td></tr>
</tbody></table>
</details>
</div>
<ul>
<li><code>fn</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> The function that is being deprecated.</li>
<li><code>msg</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> A warning message to display when the deprecated function is
invoked.</li>
<li><code>code</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> A deprecation code. See the <a href="deprecations.html#deprecations_list_of_deprecated_apis">list of deprecated APIs</a> for a
list of codes.</li>
<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> The deprecated function wrapped to emit a warning.</li>
</ul>
<p>The <code>util.deprecate()</code> method wraps <code>fn</code> (which may be a function or class) in
such a way that it is marked as deprecated.</p>
<pre><code class="language-js"><span class="hljs-keyword">const</span> util = <span class="hljs-built_in">require</span>(<span class="hljs-string">'util'</span>);
exports.obsoleteFunction = util.deprecate(<span class="hljs-function"><span class="hljs-params">()</span> =></span> {
<span class="hljs-comment">// Do something here.</span>
}, <span class="hljs-string">'obsoleteFunction() is deprecated. Use newShinyFunction() instead.'</span>);</code></pre>
<p>When called, <code>util.deprecate()</code> will return a function that will emit a
<code>DeprecationWarning</code> using the <a href="process.html#process_event_warning"><code>'warning'</code></a> event. The warning will
be emitted and printed to <code>stderr</code> the first time the returned function is
called. After the warning is emitted, the wrapped function is called without
emitting a warning.</p>
<p>If the same optional <code>code</code> is supplied in multiple calls to <code>util.deprecate()</code>,
the warning will be emitted only once for that <code>code</code>.</p>
<pre><code class="language-js"><span class="hljs-keyword">const</span> util = <span class="hljs-built_in">require</span>(<span class="hljs-string">'util'</span>);
<span class="hljs-keyword">const</span> fn1 = util.deprecate(someFunction, someMessage, <span class="hljs-string">'DEP0001'</span>);
<span class="hljs-keyword">const</span> fn2 = util.deprecate(someOtherFunction, someOtherMessage, <span class="hljs-string">'DEP0001'</span>);
fn1(); <span class="hljs-comment">// emits a deprecation warning with code DEP0001</span>
fn2(); <span class="hljs-comment">// does not emit a deprecation warning because it has the same code</span></code></pre>
<p>If either the <code>--no-deprecation</code> or <code>--no-warnings</code> command line flags are
used, or if the <code>process.noDeprecation</code> property is set to <code>true</code> <em>prior</em> to
the first deprecation warning, the <code>util.deprecate()</code> method does nothing.</p>
<p>If the <code>--trace-deprecation</code> or <code>--trace-warnings</code> command line flags are set,
or the <code>process.traceDeprecation</code> property is set to <code>true</code>, a warning and a
stack trace are printed to <code>stderr</code> the first time the deprecated function is
called.</p>
<p>If the <code>--throw-deprecation</code> command line flag is set, or the
<code>process.throwDeprecation</code> property is set to <code>true</code>, then an exception will be
thrown when the deprecated function is called.</p>
<p>The <code>--throw-deprecation</code> command line flag and <code>process.throwDeprecation</code>
property take precedence over <code>--trace-deprecation</code> and
<code>process.traceDeprecation</code>.</p>
<h2>util.format(format[, ...args])<a class="srclink" href="https://github.com/runkitdev/node/blob/cf1ce2bcc1a88ef6ff1bf745a19913a69607fb02/lib/util.js#L71">[src]</a><span><a class="mark" href="#util_util_format_format_args" id="util_util_format_format_args">#</a></span></h2>
<div class="api_metadata">
<details class="changelog"><summary>History</summary>
<table>
<tbody><tr><th>Version</th><th>Changes</th></tr>
<tr><td>REPLACEME</td>
<td><p>The <code>format</code> argument is now only taken as such if it actually contains format specifiers.</p></td></tr>
<tr><td>REPLACEME</td>
<td><p>If the <code>format</code> argument is not a format string, the output string's formatting is no longer dependent on the type of the first argument. This change removes previously present quotes from strings that were being output when the first argument was not a string.</p></td></tr>
<tr><td>v11.0.0</td>
<td><p>The <code>%o</code> specifier's <code>depth</code> option will now fall back to the default depth.</p></td></tr>
<tr><td>v8.4.0</td>
<td><p>The <code>%o</code> and <code>%O</code> specifiers are supported now.</p></td></tr>
<tr><td>v0.5.3</td>
<td><p><span>Added in: v0.5.3</span></p></td></tr>
</tbody></table>
</details>
</div>
<ul>
<li><code>format</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> A <code>printf</code>-like format string.</li>
</ul>
<p>The <code>util.format()</code> method returns a formatted string using the first argument
as a <code>printf</code>-like format string which can contain zero or more format
specifiers. Each specifier is replaced with the converted value from the
corresponding argument. Supported specifiers are:</p>
<ul>
<li><code>%s</code> - <code>String</code>.</li>
<li><code>%d</code> - <code>Number</code> (integer or floating point value) or <code>BigInt</code>.</li>
<li><code>%i</code> - Integer or <code>BigInt</code>.</li>
<li><code>%f</code> - Floating point value.</li>
<li><code>%j</code> - JSON. Replaced with the string <code>'[Circular]'</code> if the argument
contains circular references.</li>
<li><code>%o</code> - <code>Object</code>. A string representation of an object
with generic JavaScript object formatting.
Similar to <code>util.inspect()</code> with options
<code>{ showHidden: true, showProxy: true }</code>. This will show the full object
including non-enumerable properties and proxies.</li>
<li><code>%O</code> - <code>Object</code>. A string representation of an object with generic JavaScript
object formatting. Similar to <code>util.inspect()</code> without options. This will show
the full object not including non-enumerable properties and proxies.</li>
<li><code>%%</code> - single percent sign (<code>'%'</code>). This does not consume an argument.</li>
<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> The formatted string</li>
</ul>
<p>If a specifier does not have a corresponding argument, it is not replaced:</p>
<pre><code class="language-js">util.format(<span class="hljs-string">'%s:%s'</span>, <span class="hljs-string">'foo'</span>);
<span class="hljs-comment">// Returns: 'foo:%s'</span></code></pre>
<p>Values that are not part of the format string are formatted using
<code>util.inspect()</code> if their type is either <code>'object'</code>, <code>'symbol'</code>, <code>'function'</code>
or <code>'number'</code> and using <code>String()</code> in all other cases.</p>
<p>If there are more arguments passed to the <code>util.format()</code> method than the
number of specifiers, the extra arguments are concatenated to the returned
string, separated by spaces:</p>
<pre><code class="language-js">util.format(<span class="hljs-string">'%s:%s'</span>, <span class="hljs-string">'foo'</span>, <span class="hljs-string">'bar'</span>, <span class="hljs-string">'baz'</span>);
<span class="hljs-comment">// Returns: 'foo:bar baz'</span></code></pre>
<p>If the first argument does not contain a valid format specifier, <code>util.format()</code>
returns a string that is the concatenation of all arguments separated by spaces:</p>
<pre><code class="language-js">util.format(<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, <span class="hljs-number">3</span>);
<span class="hljs-comment">// Returns: '1 2 3'</span></code></pre>
<p>If only one argument is passed to <code>util.format()</code>, it is returned as it is
without any formatting:</p>
<pre><code class="language-js">util.format(<span class="hljs-string">'%% %s'</span>);
<span class="hljs-comment">// Returns: '%% %s'</span></code></pre>
<p>Please note that <code>util.format()</code> is a synchronous method that is mainly
intended as a debugging tool. Some input values can have a significant
performance overhead that can block the event loop. Use this function
with care and never in a hot code path.</p>
<h2>util.formatWithOptions(inspectOptions, format[, ...args])<a class="srclink" href="https://github.com/runkitdev/node/blob/cf1ce2bcc1a88ef6ff1bf745a19913a69607fb02/lib/util.js#L85">[src]</a><span><a class="mark" href="#util_util_formatwithoptions_inspectoptions_format_args" id="util_util_formatwithoptions_inspectoptions_format_args">#</a></span></h2>
<div class="api_metadata">
<span>Added in: v10.0.0</span>
</div>
<ul>
<li><code>inspectOptions</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a></li>
<li><code>format</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li>
</ul>
<p>This function is identical to <a href="#util_util_format_format_args"><code>util.format()</code></a>, except in that it takes
an <code>inspectOptions</code> argument which specifies options that are passed along to
<a href="#util_util_inspect_object_options"><code>util.inspect()</code></a>.</p>
<pre><code class="language-js">util.formatWithOptions({ <span class="hljs-attr">colors</span>: <span class="hljs-literal">true</span> }, <span class="hljs-string">'See object %O'</span>, { <span class="hljs-attr">foo</span>: <span class="hljs-number">42</span> });
<span class="hljs-comment">// Returns 'See object { foo: 42 }', where `42` is colored as a number</span>
<span class="hljs-comment">// when printed to a terminal.</span></code></pre>
<h2>util.getSystemErrorName(err)<a class="srclink" href="https://github.com/runkitdev/node/blob/cf1ce2bcc1a88ef6ff1bf745a19913a69607fb02/lib/util.js#L397">[src]</a><span><a class="mark" href="#util_util_getsystemerrorname_err" id="util_util_getsystemerrorname_err">#</a></span></h2>
<div class="api_metadata">
<span>Added in: v9.7.0</span>
</div>
<ul>
<li><code>err</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a></li>
<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a></li>
</ul>
<p>Returns the string name for a numeric error code that comes from a Node.js API.
The mapping between error codes and error names is platform-dependent.
See <a href="errors.html#errors_common_system_errors">Common System Errors</a> for the names of common errors.</p>
<pre><code class="language-js">fs.access(<span class="hljs-string">'file/that/does/not/exist'</span>, (err) => {
<span class="hljs-keyword">const</span> name = util.getSystemErrorName(err.errno);
<span class="hljs-built_in">console</span>.error(name); <span class="hljs-comment">// ENOENT</span>
});</code></pre>
<h2>util.inherits(constructor, superConstructor)<a class="srclink" href="https://github.com/runkitdev/node/blob/cf1ce2bcc1a88ef6ff1bf745a19913a69607fb02/lib/util.js#L300">[src]</a><span><a class="mark" href="#util_util_inherits_constructor_superconstructor" id="util_util_inherits_constructor_superconstructor">#</a></span></h2>
<div class="api_metadata">
<details class="changelog"><summary>History</summary>
<table>
<tbody><tr><th>Version</th><th>Changes</th></tr>
<tr><td>v5.0.0</td>
<td><p>The <code>constructor</code> parameter can refer to an ES6 class now.</p></td></tr>
<tr><td>v0.3.0</td>
<td><p><span>Added in: v0.3.0</span></p></td></tr>
</tbody></table>
</details>
</div>
<ul>
<li><code>constructor</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a></li>
<li><code>superConstructor</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a></li>
</ul>
<p>Usage of <code>util.inherits()</code> is discouraged. Please use the ES6 <code>class</code> and
<code>extends</code> keywords to get language level inheritance support. Also note
that the two styles are <a href="https://github.com/nodejs/node/issues/4179">semantically incompatible</a>.</p>
<p>Inherit the prototype methods from one <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/constructor">constructor</a> into another. The
prototype of <code>constructor</code> will be set to a new object created from
<code>superConstructor</code>.</p>
<p>As an additional convenience, <code>superConstructor</code> will be accessible
through the <code>constructor.super_</code> property.</p>
<pre><code class="language-js"><span class="hljs-keyword">const</span> util = <span class="hljs-built_in">require</span>(<span class="hljs-string">'util'</span>);
<span class="hljs-keyword">const</span> EventEmitter = <span class="hljs-built_in">require</span>(<span class="hljs-string">'events'</span>);
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">MyStream</span>(<span class="hljs-params"></span>) </span>{
EventEmitter.call(<span class="hljs-keyword">this</span>);
}
util.inherits(MyStream, EventEmitter);
MyStream.prototype.write = <span class="hljs-function"><span class="hljs-keyword">function</span>(<span class="hljs-params">data</span>) </span>{
<span class="hljs-keyword">this</span>.emit(<span class="hljs-string">'data'</span>, data);
};
<span class="hljs-keyword">const</span> stream = <span class="hljs-keyword">new</span> MyStream();
<span class="hljs-built_in">console</span>.log(stream <span class="hljs-keyword">instanceof</span> EventEmitter); <span class="hljs-comment">// true</span>
<span class="hljs-built_in">console</span>.log(MyStream.super_ === EventEmitter); <span class="hljs-comment">// true</span>
stream.on(<span class="hljs-string">'data'</span>, (data) => {
<span class="hljs-built_in">console</span>.log(<span class="hljs-string">`Received data: "<span class="hljs-subst">${data}</span>"`</span>);
});
stream.write(<span class="hljs-string">'It works!'</span>); <span class="hljs-comment">// Received data: "It works!"</span></code></pre>
<p>ES6 example using <code>class</code> and <code>extends</code>:</p>
<pre><code class="language-js"><span class="hljs-keyword">const</span> EventEmitter = <span class="hljs-built_in">require</span>(<span class="hljs-string">'events'</span>);
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">MyStream</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">EventEmitter</span> </span>{
write(data) {
<span class="hljs-keyword">this</span>.emit(<span class="hljs-string">'data'</span>, data);
}
}
<span class="hljs-keyword">const</span> stream = <span class="hljs-keyword">new</span> MyStream();
stream.on(<span class="hljs-string">'data'</span>, (data) => {
<span class="hljs-built_in">console</span>.log(<span class="hljs-string">`Received data: "<span class="hljs-subst">${data}</span>"`</span>);
});
stream.write(<span class="hljs-string">'With ES6'</span>);</code></pre>
<h2>util.inspect(object[, options])<span><a class="mark" href="#util_util_inspect_object_options" id="util_util_inspect_object_options">#</a></span></h2>
<h2>util.inspect(object[, showHidden[, depth[, colors]]])<span><a class="mark" href="#util_util_inspect_object_showhidden_depth_colors" id="util_util_inspect_object_showhidden_depth_colors">#</a></span></h2>
<div class="api_metadata">
<details class="changelog"><summary>History</summary>
<table>
<tbody><tr><th>Version</th><th>Changes</th></tr>
<tr><td>v11.0.0</td>
<td><p>The <code>depth</code> default changed to <code>20</code>.</p></td></tr>
<tr><td>v11.0.0</td>
<td><p>The inspection output is now limited to about 128 MB. Data above that size will not be fully inspected.</p></td></tr>
<tr><td>v10.12.0</td>
<td><p>The <code>sorted</code> option is supported now.</p></td></tr>
<tr><td>v10.6.0</td>
<td><p>Inspecting linked lists and similar objects is now possible up to the maximum call stack size.</p></td></tr>
<tr><td>v10.0.0</td>
<td><p>The <code>WeakMap</code> and <code>WeakSet</code> entries can now be inspected as well.</p></td></tr>
<tr><td>v9.9.0</td>
<td><p>The <code>compact</code> option is supported now.</p></td></tr>
<tr><td>v6.6.0</td>
<td><p>Custom inspection functions can now return <code>this</code>.</p></td></tr>
<tr><td>v6.3.0</td>
<td><p>The <code>breakLength</code> option is supported now.</p></td></tr>
<tr><td>v6.1.0</td>
<td><p>The <code>maxArrayLength</code> option is supported now; in particular, long arrays are truncated by default.</p></td></tr>
<tr><td>v6.1.0</td>
<td><p>The <code>showProxy</code> option is supported now.</p></td></tr>
<tr><td>v0.3.0</td>
<td><p><span>Added in: v0.3.0</span></p></td></tr>
</tbody></table>
</details>
</div>
<ul>
<li><code>object</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a> Any JavaScript primitive or <code>Object</code>.</li>
<li>
<p><code>options</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object" class="type"><Object></a></p>
<ul>
<li><code>showHidden</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> If <code>true</code>, the <code>object</code>'s non-enumerable symbols and
properties will be included in the formatted result as well as <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WeakMap"><code>WeakMap</code></a>
and <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WeakSet"><code>WeakSet</code></a> entries. <strong>Default:</strong> <code>false</code>.</li>
<li><code>depth</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><number></a> Specifies the number of times to recurse while formatting
the <code>object</code>. This is useful for inspecting large complicated objects. To
make it recurse up to the maximum call stack size pass <code>Infinity</code> or <code>null</code>.
<strong>Default:</strong> <code>2</code>.</li>
<li><code>colors</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> If <code>true</code>, the output will be styled with ANSI color
codes. Colors are customizable, see <a href="#util_customizing_util_inspect_colors">Customizing <code>util.inspect</code> colors</a>.
<strong>Default:</strong> <code>false</code>.</li>
<li><code>customInspect</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> If <code>false</code>, then
<code>[util.inspect.custom](depth, opts)</code> functions will not be called.
<strong>Default:</strong> <code>true</code>.</li>
<li>
<p><code>showProxy</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> If <code>true</code>, then objects and functions that are
<code>Proxy</code> objects will be introspected to show their <code>target</code> and <code>handler</code>
objects. <strong>Default:</strong> <code>false</code>.</p>
<!--
TODO(BridgeAR): Deprecate `maxArrayLength` and replace it with
`maxEntries`.
-->
</li>
<li><code>maxArrayLength</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> Specifies the maximum number of <code>Array</code>,
<a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/TypedArray"><code>TypedArray</code></a>, <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WeakMap"><code>WeakMap</code></a> and <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WeakSet"><code>WeakSet</code></a> elements to include when
formatting. Set to <code>null</code> or <code>Infinity</code> to show all elements. Set to <code>0</code> or
negative to show no elements. <strong>Default:</strong> <code>100</code>.</li>
<li><code>breakLength</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Number_type" class="type"><integer></a> The length at which an object's keys are split
across multiple lines. Set to <code>Infinity</code> to format an object as a single
line. <strong>Default:</strong> <code>60</code> for legacy compatibility.</li>
<li><code>compact</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> Setting this to <code>false</code> changes the default indentation
to use a line break for each object key instead of lining up multiple
properties in one line. It will also break text that is above the
<code>breakLength</code> size into smaller and better readable chunks and indents
objects the same as arrays. Note that no text will be reduced below 16
characters, no matter the <code>breakLength</code> size. For more information, see the
example below. <strong>Default:</strong> <code>true</code>.</li>
<li><code>sorted</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a> | <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a> If set to <code>true</code> or a function, all properties
of an object and Set and Map entries will be sorted in the returned string.
If set to <code>true</code> the <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort">default sort</a> is going to be used. If set to a
function, it is used as a <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/sort#Parameters">compare function</a>.</li>
</ul>
</li>
<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#String_type" class="type"><string></a> The representation of passed object</li>
</ul>
<p>The <code>util.inspect()</code> method returns a string representation of <code>object</code> that is
intended for debugging. The output of <code>util.inspect</code> may change at any time
and should not be depended upon programmatically. Additional <code>options</code> may be
passed that alter certain aspects of the formatted string.
<code>util.inspect()</code> will use the constructor's name and/or <code>@@toStringTag</code> to make
an identifiable tag for an inspected value.</p>
<pre><code class="language-js"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Foo</span> </span>{
get [<span class="hljs-built_in">Symbol</span>.toStringTag]() {
<span class="hljs-keyword">return</span> <span class="hljs-string">'bar'</span>;
}
}
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Bar</span> </span>{}
<span class="hljs-keyword">const</span> baz = <span class="hljs-built_in">Object</span>.create(<span class="hljs-literal">null</span>, { [<span class="hljs-built_in">Symbol</span>.toStringTag]: { <span class="hljs-attr">value</span>: <span class="hljs-string">'foo'</span> } });
util.inspect(<span class="hljs-keyword">new</span> Foo()); <span class="hljs-comment">// 'Foo [bar] {}'</span>
util.inspect(<span class="hljs-keyword">new</span> Bar()); <span class="hljs-comment">// 'Bar {}'</span>
util.inspect(baz); <span class="hljs-comment">// '[foo] {}'</span></code></pre>
<p>The following example inspects all properties of the <code>util</code> object:</p>
<pre><code class="language-js"><span class="hljs-keyword">const</span> util = <span class="hljs-built_in">require</span>(<span class="hljs-string">'util'</span>);
<span class="hljs-built_in">console</span>.log(util.inspect(util, { <span class="hljs-attr">showHidden</span>: <span class="hljs-literal">true</span>, <span class="hljs-attr">depth</span>: <span class="hljs-literal">null</span> }));</code></pre>
<p>The following example highlights the difference with the <code>compact</code> option:</p>
<pre><code class="language-js"><span class="hljs-keyword">const</span> util = <span class="hljs-built_in">require</span>(<span class="hljs-string">'util'</span>);
<span class="hljs-keyword">const</span> o = {
<span class="hljs-attr">a</span>: [<span class="hljs-number">1</span>, <span class="hljs-number">2</span>, [[
<span class="hljs-string">'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do '</span> +
<span class="hljs-string">'eiusmod tempor incididunt ut labore et dolore magna aliqua.'</span>,
<span class="hljs-string">'test'</span>,
<span class="hljs-string">'foo'</span>]], <span class="hljs-number">4</span>],
<span class="hljs-attr">b</span>: <span class="hljs-keyword">new</span> <span class="hljs-built_in">Map</span>([[<span class="hljs-string">'za'</span>, <span class="hljs-number">1</span>], [<span class="hljs-string">'zb'</span>, <span class="hljs-string">'test'</span>]])
};
<span class="hljs-built_in">console</span>.log(util.inspect(o, { <span class="hljs-attr">compact</span>: <span class="hljs-literal">true</span>, <span class="hljs-attr">depth</span>: <span class="hljs-number">5</span>, <span class="hljs-attr">breakLength</span>: <span class="hljs-number">80</span> }));
<span class="hljs-comment">// This will print</span>
<span class="hljs-comment">// { a:</span>
<span class="hljs-comment">// [ 1,</span>
<span class="hljs-comment">// 2,</span>
<span class="hljs-comment">// [ [ 'Lorem ipsum dolor sit amet, consectetur [...]', // A long line</span>
<span class="hljs-comment">// 'test',</span>
<span class="hljs-comment">// 'foo' ] ],</span>
<span class="hljs-comment">// 4 ],</span>
<span class="hljs-comment">// b: Map { 'za' => 1, 'zb' => 'test' } }</span>
<span class="hljs-comment">// Setting `compact` to false changes the output to be more reader friendly.</span>
<span class="hljs-built_in">console</span>.log(util.inspect(o, { <span class="hljs-attr">compact</span>: <span class="hljs-literal">false</span>, <span class="hljs-attr">depth</span>: <span class="hljs-number">5</span>, <span class="hljs-attr">breakLength</span>: <span class="hljs-number">80</span> }));
<span class="hljs-comment">// {</span>
<span class="hljs-comment">// a: [</span>
<span class="hljs-comment">// 1,</span>
<span class="hljs-comment">// 2,</span>
<span class="hljs-comment">// [</span>
<span class="hljs-comment">// [</span>
<span class="hljs-comment">// 'Lorem ipsum dolor sit amet, consectetur ' +</span>
<span class="hljs-comment">// 'adipiscing elit, sed do eiusmod tempor ' +</span>
<span class="hljs-comment">// 'incididunt ut labore et dolore magna ' +</span>
<span class="hljs-comment">// 'aliqua.,</span>
<span class="hljs-comment">// 'test',</span>
<span class="hljs-comment">// 'foo'</span>
<span class="hljs-comment">// ]</span>
<span class="hljs-comment">// ],</span>
<span class="hljs-comment">// 4</span>
<span class="hljs-comment">// ],</span>
<span class="hljs-comment">// b: Map {</span>
<span class="hljs-comment">// 'za' => 1,</span>
<span class="hljs-comment">// 'zb' => 'test'</span>
<span class="hljs-comment">// }</span>
<span class="hljs-comment">// }</span>
<span class="hljs-comment">// Setting `breakLength` to e.g. 150 will print the "Lorem ipsum" text in a</span>
<span class="hljs-comment">// single line.</span>
<span class="hljs-comment">// Reducing the `breakLength` will split the "Lorem ipsum" text in smaller</span>
<span class="hljs-comment">// chunks.</span></code></pre>
<p>Using the <code>showHidden</code> option allows to inspect <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WeakMap"><code>WeakMap</code></a> and <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WeakSet"><code>WeakSet</code></a>
entries. If there are more entries than <code>maxArrayLength</code>, there is no guarantee
which entries are displayed. That means retrieving the same <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/WeakSet"><code>WeakSet</code></a>
entries twice might actually result in a different output. Besides this any item
might be collected at any point of time by the garbage collector if there is no
strong reference left to that object. Therefore there is no guarantee to get a
reliable output.</p>
<pre><code class="language-js"><span class="hljs-keyword">const</span> { inspect } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'util'</span>);
<span class="hljs-keyword">const</span> obj = { <span class="hljs-attr">a</span>: <span class="hljs-number">1</span> };
<span class="hljs-keyword">const</span> obj2 = { <span class="hljs-attr">b</span>: <span class="hljs-number">2</span> };
<span class="hljs-keyword">const</span> weakSet = <span class="hljs-keyword">new</span> <span class="hljs-built_in">WeakSet</span>([obj, obj2]);
<span class="hljs-built_in">console</span>.log(inspect(weakSet, { <span class="hljs-attr">showHidden</span>: <span class="hljs-literal">true</span> }));
<span class="hljs-comment">// WeakSet { { a: 1 }, { b: 2 } }</span></code></pre>
<p>The <code>sorted</code> option makes sure the output is identical, no matter of the
properties insertion order:</p>
<pre><code class="language-js"><span class="hljs-keyword">const</span> { inspect } = <span class="hljs-built_in">require</span>(<span class="hljs-string">'util'</span>);
<span class="hljs-keyword">const</span> assert = <span class="hljs-built_in">require</span>(<span class="hljs-string">'assert'</span>);
<span class="hljs-keyword">const</span> o1 = {
<span class="hljs-attr">b</span>: [<span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-number">1</span>],
<span class="hljs-attr">a</span>: <span class="hljs-string">'`a` comes before `b`'</span>,
<span class="hljs-attr">c</span>: <span class="hljs-keyword">new</span> <span class="hljs-built_in">Set</span>([<span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-number">1</span>])
};
<span class="hljs-built_in">console</span>.log(inspect(o1, { <span class="hljs-attr">sorted</span>: <span class="hljs-literal">true</span> }));
<span class="hljs-comment">// { a: '`a` comes before `b`', b: [ 2, 3, 1 ], c: Set { 1, 2, 3 } }</span>
<span class="hljs-built_in">console</span>.log(inspect(o1, { <span class="hljs-attr">sorted</span>: <span class="hljs-function">(<span class="hljs-params">a, b</span>) =></span> b.localeCompare(a) }));
<span class="hljs-comment">// { c: Set { 3, 2, 1 }, b: [ 2, 3, 1 ], a: '`a` comes before `b`' }</span>
<span class="hljs-keyword">const</span> o2 = {
<span class="hljs-attr">c</span>: <span class="hljs-keyword">new</span> <span class="hljs-built_in">Set</span>([<span class="hljs-number">2</span>, <span class="hljs-number">1</span>, <span class="hljs-number">3</span>]),
<span class="hljs-attr">a</span>: <span class="hljs-string">'`a` comes before `b`'</span>,
<span class="hljs-attr">b</span>: [<span class="hljs-number">2</span>, <span class="hljs-number">3</span>, <span class="hljs-number">1</span>]
};
assert.strict.equal(
inspect(o1, { <span class="hljs-attr">sorted</span>: <span class="hljs-literal">true</span> }),
inspect(o2, { <span class="hljs-attr">sorted</span>: <span class="hljs-literal">true</span> })
);</code></pre>
<p>Please note that <code>util.inspect()</code> is a synchronous method that is mainly
intended as a debugging tool. Its maximum output length is limited to
approximately 128 MB and input values that result in output bigger than that
will not be inspected fully. Such values can have a significant performance
overhead that can block the event loop for a significant amount of time.</p>
<h3>Customizing `util.inspect` colors<span><a class="mark" href="#util_customizing_util_inspect_colors" id="util_customizing_util_inspect_colors">#</a></span></h3>
<p>Color output (if enabled) of <code>util.inspect</code> is customizable globally
via the <code>util.inspect.styles</code> and <code>util.inspect.colors</code> properties.</p>
<p><code>util.inspect.styles</code> is a map associating a style name to a color from
<code>util.inspect.colors</code>.</p>
<p>The default styles and associated colors are:</p>
<ul>
<li><code>number</code> - <code>yellow</code></li>
<li><code>boolean</code> - <code>yellow</code></li>
<li><code>string</code> - <code>green</code></li>
<li><code>date</code> - <code>magenta</code></li>
<li><code>regexp</code> - <code>red</code></li>
<li><code>null</code> - <code>bold</code></li>
<li><code>undefined</code> - <code>grey</code></li>
<li><code>special</code> - <code>cyan</code> (only applied to functions at this time)</li>
<li><code>name</code> - (no styling)</li>
</ul>
<p>The predefined color codes are: <code>white</code>, <code>grey</code>, <code>black</code>, <code>blue</code>, <code>cyan</code>,
<code>green</code>, <code>magenta</code>, <code>red</code> and <code>yellow</code>. There are also <code>bold</code>, <code>italic</code>,
<code>underline</code> and <code>inverse</code> codes.</p>
<p>Color styling uses ANSI control codes that may not be supported on all
terminals.</p>
<h3>Custom inspection functions on Objects<span><a class="mark" href="#util_custom_inspection_functions_on_objects" id="util_custom_inspection_functions_on_objects">#</a></span></h3>
<p>Objects may also define their own
<a href="#util_util_inspect_custom"><code>[util.inspect.custom](depth, opts)</code></a> function,
which <code>util.inspect()</code> will invoke and use the result of when inspecting
the object:</p>
<pre><code class="language-js"><span class="hljs-keyword">const</span> util = <span class="hljs-built_in">require</span>(<span class="hljs-string">'util'</span>);
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Box</span> </span>{
<span class="hljs-keyword">constructor</span>(value) {
<span class="hljs-keyword">this</span>.value = value;
}
[util.inspect.custom](depth, options) {
<span class="hljs-keyword">if</span> (depth < <span class="hljs-number">0</span>) {
<span class="hljs-keyword">return</span> options.stylize(<span class="hljs-string">'[Box]'</span>, <span class="hljs-string">'special'</span>);
}
<span class="hljs-keyword">const</span> newOptions = <span class="hljs-built_in">Object</span>.assign({}, options, {
<span class="hljs-attr">depth</span>: options.depth === <span class="hljs-literal">null</span> ? <span class="hljs-literal">null</span> : options.depth - <span class="hljs-number">1</span>
});
<span class="hljs-comment">// Five space padding because that's the size of "Box< ".</span>
<span class="hljs-keyword">const</span> padding = <span class="hljs-string">' '</span>.repeat(<span class="hljs-number">5</span>);
<span class="hljs-keyword">const</span> inner = util.inspect(<span class="hljs-keyword">this</span>.value, newOptions)
.replace(<span class="hljs-regexp">/\n/g</span>, <span class="hljs-string">`\n<span class="hljs-subst">${padding}</span>`</span>);
<span class="hljs-keyword">return</span> <span class="hljs-string">`<span class="hljs-subst">${options.stylize(<span class="hljs-string">'Box'</span>, <span class="hljs-string">'special'</span>)}</span>< <span class="hljs-subst">${inner}</span> >`</span>;
}
}
<span class="hljs-keyword">const</span> box = <span class="hljs-keyword">new</span> Box(<span class="hljs-literal">true</span>);
util.inspect(box);
<span class="hljs-comment">// Returns: "Box< true >"</span></code></pre>
<p>Custom <code>[util.inspect.custom](depth, opts)</code> functions typically return a string
but may return a value of any type that will be formatted accordingly by
<code>util.inspect()</code>.</p>
<pre><code class="language-js"><span class="hljs-keyword">const</span> util = <span class="hljs-built_in">require</span>(<span class="hljs-string">'util'</span>);
<span class="hljs-keyword">const</span> obj = { <span class="hljs-attr">foo</span>: <span class="hljs-string">'this will not show up in the inspect() output'</span> };
obj[util.inspect.custom] = <span class="hljs-function">(<span class="hljs-params">depth</span>) =></span> {
<span class="hljs-keyword">return</span> { <span class="hljs-attr">bar</span>: <span class="hljs-string">'baz'</span> };
};
util.inspect(obj);
<span class="hljs-comment">// Returns: "{ bar: 'baz' }"</span></code></pre>
<h3>util.inspect.custom<span><a class="mark" href="#util_util_inspect_custom" id="util_util_inspect_custom">#</a></span></h3>
<div class="api_metadata">
<details class="changelog"><summary>History</summary>
<table>
<tbody><tr><th>Version</th><th>Changes</th></tr>
<tr><td>v10.12.0</td>
<td><p>This is now defined as a shared symbol.</p></td></tr>
<tr><td>v6.6.0</td>
<td><p><span>Added in: v6.6.0</span></p></td></tr>
</tbody></table>
</details>
</div>
<ul>
<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Symbol_type" class="type"><symbol></a> that can be used to declare custom inspect functions.</li>
</ul>
<p>In addition to being accessible through <code>util.inspect.custom</code>, this
symbol is <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Symbol/for">registered globally</a> and can be
accessed in any environment as <code>Symbol.for('nodejs.util.inspect.custom')</code>.</p>
<pre><code class="language-js"><span class="hljs-keyword">const</span> inspect = <span class="hljs-built_in">Symbol</span>.for(<span class="hljs-string">'nodejs.util.inspect.custom'</span>);
<span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Password</span> </span>{
<span class="hljs-keyword">constructor</span>(value) {
<span class="hljs-keyword">this</span>.value = value;
}
toString() {
<span class="hljs-keyword">return</span> <span class="hljs-string">'xxxxxxxx'</span>;
}
[inspect]() {
<span class="hljs-keyword">return</span> <span class="hljs-string">`Password <<span class="hljs-subst">${<span class="hljs-keyword">this</span>.toString()}</span>>`</span>;
}
}
<span class="hljs-keyword">const</span> password = <span class="hljs-keyword">new</span> Password(<span class="hljs-string">'r0sebud'</span>);
<span class="hljs-built_in">console</span>.log(password);
<span class="hljs-comment">// Prints Password <xxxxxxxx></span></code></pre>
<p>See <a href="#util_custom_inspection_functions_on_objects">Custom inspection functions on Objects</a> for more details.</p>
<h3>util.inspect.defaultOptions<span><a class="mark" href="#util_util_inspect_defaultoptions" id="util_util_inspect_defaultoptions">#</a></span></h3>
<div class="api_metadata">
<span>Added in: v6.4.0</span>
</div>
<p>The <code>defaultOptions</code> value allows customization of the default options used by
<code>util.inspect</code>. This is useful for functions like <code>console.log</code> or
<code>util.format</code> which implicitly call into <code>util.inspect</code>. It shall be set to an
object containing one or more valid <a href="#util_util_inspect_object_options"><code>util.inspect()</code></a> options. Setting
option properties directly is also supported.</p>
<pre><code class="language-js"><span class="hljs-keyword">const</span> util = <span class="hljs-built_in">require</span>(<span class="hljs-string">'util'</span>);
<span class="hljs-keyword">const</span> arr = <span class="hljs-built_in">Array</span>(<span class="hljs-number">101</span>).fill(<span class="hljs-number">0</span>);
<span class="hljs-built_in">console</span>.log(arr); <span class="hljs-comment">// logs the truncated array</span>
util.inspect.defaultOptions.maxArrayLength = <span class="hljs-literal">null</span>;
<span class="hljs-built_in">console</span>.log(arr); <span class="hljs-comment">// logs the full array</span></code></pre>
<h2>util.isDeepStrictEqual(val1, val2)<span><a class="mark" href="#util_util_isdeepstrictequal_val1_val2" id="util_util_isdeepstrictequal_val1_val2">#</a></span></h2>
<div class="api_metadata">
<span>Added in: v9.0.0</span>
</div>
<ul>
<li><code>val1</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li>
<li><code>val2</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Data_types" class="type"><any></a></li>
<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Boolean_type" class="type"><boolean></a></li>
</ul>
<p>Returns <code>true</code> if there is deep strict equality between <code>val1</code> and <code>val2</code>.
Otherwise, returns <code>false</code>.</p>
<p>See <a href="assert.html#assert_assert_deepstrictequal_actual_expected_message"><code>assert.deepStrictEqual()</code></a> for more information about deep strict
equality.</p>
<h2>util.promisify(original)<span><a class="mark" href="#util_util_promisify_original" id="util_util_promisify_original">#</a></span></h2>
<div class="api_metadata">
<span>Added in: v8.0.0</span>
</div>
<ul>
<li><code>original</code> <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a></li>
<li>Returns: <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Function" class="type"><Function></a></li>
</ul>
<p>Takes a function following the common error-first callback style, i.e. taking
an <code>(err, value) => ...</code> callback as the last argument, and returns a version
that returns promises.</p>
<pre><code class="language-js"><span class="hljs-keyword">const</span> util = <span class="hljs-built_in">require</span>(<span class="hljs-string">'util'</span>);
<span class="hljs-keyword">const</span> fs = <span class="hljs-built_in">require</span>(<span class="hljs-string">'fs'</span>);
<span class="hljs-keyword">const</span> stat = util.promisify(fs.stat);
stat(<span class="hljs-string">'.'</span>).then(<span class="hljs-function">(<span class="hljs-params">stats</span>) =></span> {
<span class="hljs-comment">// Do something with `stats`</span>
}).catch(<span class="hljs-function">(<span class="hljs-params">error</span>) =></span> {
<span class="hljs-comment">// Handle the error.</span>
});</code></pre>
<p>Or, equivalently using <code>async function</code>s:</p>
<pre><code class="language-js"><span class="hljs-keyword">const</span> util = <span class="hljs-built_in">require</span>(<span class="hljs-string">'util'</span>);
<span class="hljs-keyword">const</span> fs = <span class="hljs-built_in">require</span>(<span class="hljs-string">'fs'</span>);
<span class="hljs-keyword">const</span> stat = util.promisify(fs.stat);
<span class="hljs-keyword">async</span> <span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">callStat</span>(<span class="hljs-params"></span>) </span>{
<span class="hljs-keyword">const</span> stats = <span class="hljs-keyword">await</span> stat(<span class="hljs-string">'.'</span>);
<span class="hljs-built_in">console</span>.log(<span class="hljs-string">`This directory is owned by <span class="hljs-subst">${stats.uid}</span>`</span>);
}</code></pre>
<p>If there is an <code>original[util.promisify.custom]</code> property present, <code>promisify</code>
will return its value, see <a href="#util_custom_promisified_functions">Custom promisified functions</a>.</p>
<p><code>promisify()</code> assumes that <code>original</code> is a function taking a callback as its
final argument in all cases. If <code>original</code> is not a function, <code>promisify()</code>
will throw an error. If <code>original</code> is a function but its last argument is not
an error-first callback, it will still be passed an error-first
callback as its last argument.</p>
<h3>Custom promisified functions<span><a class="mark" href="#util_custom_promisified_functions" id="util_custom_promisified_functions">#</a></span></h3>
<p>Using the <code>util.promisify.custom</code> symbol one can override the return value of
<a href="#util_util_promisify_original"><code>util.promisify()</code></a>:</p>
<pre><code class="language-js"><span class="hljs-keyword">const</span> util = <span class="hljs-built_in">require</span>(<span class="hljs-string">'util'</span>);
<span class="hljs-function"><span class="hljs-keyword">function</span> <span class="hljs-title">doSomething</span>(<span class="hljs-params">foo, callback</span>) </span>{
<span class="hljs-comment">// ...</span>
}
doSomething[util.promisify.custom] = <span class="hljs-function">(<span class="hljs-params">foo</span>) =></span> {
<span class="hljs-keyword">return</span> getPromiseSomehow();
};
<span class="hljs-keyword">const</span> promisified = util.promisify(doSomething);
<span class="hljs-built_in">console</span>.log(promisified === doSomething[util.promisify.custom]);
<span class="hljs-comment">// prints 'true'</span></code></pre>
<p>This can be useful for cases where the original function does not follow the
standard format of taking an error-first callback as the last argument.</p>
<p>For example, with a function that takes in
<code>(foo, onSuccessCallback, onErrorCallback)</code>:</p>
<pre><code class="language-js">doSomething[util.promisify.custom] = <span class="hljs-function">(<span class="hljs-params">foo</span>) =></span> {
<span class="hljs-keyword">return</span> <span class="hljs-keyword">new</span> <span class="hljs-built_in">Promise</span>(<span class="hljs-function">(<span class="hljs-params">resolve, reject</span>) =></span> {
doSomething(foo, resolve, reject);
});
};</code></pre>
<p>If <code>promisify.custom</code> is defined but is not a function, <code>promisify()</code> will
throw an error.</p>
<h3>util.promisify.custom<span><a class="mark" href="#util_util_promisify_custom" id="util_util_promisify_custom">#</a></span></h3>
<div class="api_metadata">
<span>Added in: v8.0.0</span>
</div>
<ul>
<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Data_structures#Symbol_type" class="type"><symbol></a> that can be used to declare custom promisified variants of functions,
see <a href="#util_custom_promisified_functions">Custom promisified functions</a>.</li>
</ul>
<h2>Class: util.TextDecoder<span><a class="mark" href="#util_class_util_textdecoder" id="util_class_util_textdecoder">#</a></span></h2>
<div class="api_metadata">
<span>Added in: v8.3.0</span>
</div>
<p>An implementation of the <a href="https://encoding.spec.whatwg.org/">WHATWG Encoding Standard</a> <code>TextDecoder</code> API.</p>
<pre><code class="language-js"><span class="hljs-keyword">const</span> decoder = <span class="hljs-keyword">new</span> TextDecoder(<span class="hljs-string">'shift_jis'</span>);
<span class="hljs-keyword">let</span> string = <span class="hljs-string">''</span>;
<span class="hljs-keyword">let</span> buffer;
<span class="hljs-keyword">while</span> (buffer = getNextChunkSomehow()) {
string += decoder.decode(buffer, { <span class="hljs-attr">stream</span>: <span class="hljs-literal">true</span> });
}
string += decoder.decode(); <span class="hljs-comment">// end-of-stream</span></code></pre>
<h3>WHATWG Supported Encodings<span><a class="mark" href="#util_whatwg_supported_encodings" id="util_whatwg_supported_encodings">#</a></span></h3>
<p>Per the <a href="https://encoding.spec.whatwg.org/">WHATWG Encoding Standard</a>, the encodings supported by the
<code>TextDecoder</code> API are outlined in the tables below. For each encoding,
one or more aliases may be used.</p>
<p>Different Node.js build configurations support different sets of encodings.
While a very basic set of encodings is supported even on Node.js builds without
ICU enabled, support for some encodings is provided only when Node.js is built
with ICU and using the full ICU data (see <a href="intl.html">Internationalization</a>).</p>
<h4>Encodings Supported Without ICU<span><a class="mark" href="#util_encodings_supported_without_icu" id="util_encodings_supported_without_icu">#</a></span></h4>
<table><thead><tr><th>Encoding</th><th>Aliases</th></tr></thead><tbody><tr><td><code>'utf-8'</code></td><td><code>'unicode-1-1-utf-8'</code>, <code>'utf8'</code></td></tr><tr><td><code>'utf-16le'</code></td><td><code>'utf-16'</code></td></tr></tbody></table>
<h4>Encodings Supported by Default (With ICU)<span><a class="mark" href="#util_encodings_supported_by_default_with_icu" id="util_encodings_supported_by_default_with_icu">#</a></span></h4>