MCPcopy Index your code
hub / github.com/nodejs/node / promisify

Function promisify

lib/internal/util.js:455–514  ·  view source on GitHub ↗
(original)

Source from the content-addressed store, hash-verified

453let validateFunction;
454
455function promisify(original) {
456 // Lazy-load to avoid a circular dependency.
457 if (validateFunction === undefined)
458 ({ validateFunction } = require('internal/validators'));
459
460 validateFunction(original, 'original');
461
462 if (original[kCustomPromisifiedSymbol]) {
463 const fn = original[kCustomPromisifiedSymbol];
464
465 validateFunction(fn, 'util.promisify.custom');
466
467 return ObjectDefineProperty(fn, kCustomPromisifiedSymbol, {
468 __proto__: null,
469 value: fn, enumerable: false, writable: false, configurable: true,
470 });
471 }
472
473 // Names to create an object from in case the callback receives multiple
474 // arguments, e.g. ['bytesRead', 'buffer'] for fs.read.
475 const argumentNames = original[kCustomPromisifyArgsSymbol];
476
477 function fn(...args) {
478 return new Promise((resolve, reject) => {
479 ArrayPrototypePush(args, (err, ...values) => {
480 if (err) {
481 return reject(err);
482 }
483 if (argumentNames !== undefined && values.length > 1) {
484 const obj = {};
485 for (let i = 0; i < argumentNames.length; i++)
486 obj[argumentNames[i]] = values[i];
487 resolve(obj);
488 } else {
489 resolve(values[0]);
490 }
491 });
492 if (isPromise(ReflectApply(original, this, args))) {
493 process.emitWarning('Calling promisify on a function that returns a Promise is likely a mistake.',
494 'DeprecationWarning', 'DEP0174');
495 }
496 });
497 }
498
499 ObjectSetPrototypeOf(fn, ObjectGetPrototypeOf(original));
500
501 ObjectDefineProperty(fn, kCustomPromisifiedSymbol, {
502 __proto__: null,
503 value: fn, enumerable: false, writable: false, configurable: true,
504 });
505
506 const descriptors = ObjectGetOwnPropertyDescriptors(original);
507 const propertiesValues = ObjectValues(descriptors);
508 for (let i = 0; i < propertiesValues.length; i++) {
509 // We want to use null-prototype objects to not rely on globally mutable
510 // %Object.prototype%.
511 ObjectSetPrototypeOf(propertiesValues[i], null);
512 }

Callers 15

net.jsFile · 0.85
dgram.jsFile · 0.85
https.jsFile · 0.85
_http_server.jsFile · 0.85
promises.jsFile · 0.85
[SymbolAsyncDispose]Method · 0.85
promises.jsFile · 0.85
constructorMethod · 0.85

Calls 1

requireFunction · 0.50

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…