index.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. 'use strict';
  2. const common = require('./common');
  3. const assert = require('assert');
  4. const fs = require('fs');
  5. const vm = require('vm');
  6. const promisify = require('..');
  7. //const customPromisifyArgs = require('..').customPromisifyArgs;
  8. const stat = promisify(fs.stat);
  9. {
  10. const promise = stat(__filename);
  11. assert(promise instanceof Promise);
  12. promise.then(common.mustCall((value) => {
  13. assert.deepStrictEqual(value, fs.statSync(__filename));
  14. }));
  15. }
  16. {
  17. const promise = stat('/dontexist');
  18. promise.catch(common.mustCall((error) => {
  19. assert(error.message.includes('ENOENT: no such file or directory, stat'));
  20. }));
  21. }
  22. {
  23. function fn() {}
  24. function promisifedFn() {}
  25. fn[promisify.custom] = promisifedFn;
  26. assert.strictEqual(promisify(fn), promisifedFn);
  27. assert.strictEqual(promisify(promisify(fn)), promisifedFn);
  28. }
  29. {
  30. function fn() {}
  31. fn[promisify.custom] = 42;
  32. assert.throws(
  33. () => promisify(fn),
  34. (err) => err instanceof TypeError &&
  35. err.message === 'The [util.promisify.custom] property must ' +
  36. 'be a function');
  37. }
  38. /*{
  39. const firstValue = 5;
  40. const secondValue = 17;
  41. function fn(callback) {
  42. callback(null, firstValue, secondValue);
  43. }
  44. fn[customPromisifyArgs] = ['first', 'second'];
  45. promisify(fn)().then(common.mustCall((obj) => {
  46. assert.deepStrictEqual(obj, {first: firstValue, second: secondValue});
  47. }));
  48. }*/
  49. {
  50. const fn = vm.runInNewContext('(function() {})');
  51. assert.notStrictEqual(Object.getPrototypeOf(promisify(fn)),
  52. Function.prototype);
  53. }
  54. {
  55. function fn(callback) {
  56. callback(null, 'foo', 'bar');
  57. }
  58. promisify(fn)().then(common.mustCall((value) => {
  59. assert.deepStrictEqual(value, 'foo');
  60. }));
  61. }
  62. {
  63. function fn(callback) {
  64. callback(null);
  65. }
  66. promisify(fn)().then(common.mustCall((value) => {
  67. assert.strictEqual(value, undefined);
  68. }));
  69. }
  70. {
  71. function fn(callback) {
  72. callback();
  73. }
  74. promisify(fn)().then(common.mustCall((value) => {
  75. assert.strictEqual(value, undefined);
  76. }));
  77. }
  78. {
  79. function fn(err, val, callback) {
  80. callback(err, val);
  81. }
  82. promisify(fn)(null, 42).then(common.mustCall((value) => {
  83. assert.strictEqual(value, 42);
  84. }));
  85. }
  86. {
  87. function fn(err, val, callback) {
  88. callback(err, val);
  89. }
  90. promisify(fn)(new Error('oops'), null).catch(common.mustCall((err) => {
  91. assert.strictEqual(err.message, 'oops');
  92. }));
  93. }
  94. if (Number(process.version[1]) >= 7) eval`
  95. {
  96. function fn(err, val, callback) {
  97. callback(err, val);
  98. }
  99. (async () => {
  100. const value = await promisify(fn)(null, 42);
  101. assert.strictEqual(value, 42);
  102. })();
  103. }`
  104. {
  105. const o = {};
  106. const fn = promisify(function(cb) {
  107. cb(null, this === o);
  108. });
  109. o.fn = fn;
  110. o.fn().then(common.mustCall(function(val) {
  111. assert(val);
  112. }));
  113. }
  114. if (Number(process.version[1]) >= 7) eval`
  115. {
  116. const err = new Error('Should not have called the callback with the error.');
  117. const stack = err.stack;
  118. const fn = promisify(function(cb) {
  119. cb(null);
  120. cb(err);
  121. });
  122. (async () => {
  123. await fn();
  124. await Promise.resolve();
  125. return assert.strictEqual(stack, err.stack);
  126. })();
  127. }`
  128. {
  129. function c() { }
  130. const a = promisify(function() { });
  131. const b = promisify(a);
  132. assert.notStrictEqual(c, a);
  133. assert.strictEqual(a, b);
  134. }
  135. {
  136. let errToThrow;
  137. const thrower = promisify(function(a, b, c, cb) {
  138. errToThrow = new Error();
  139. throw errToThrow;
  140. });
  141. thrower(1, 2, 3)
  142. .then(assert.fail)
  143. .then(assert.fail, (e) => assert.strictEqual(e, errToThrow));
  144. }
  145. {
  146. const err = new Error();
  147. const a = promisify((cb) => cb(err))();
  148. const b = promisify(() => { throw err; })();
  149. Promise.all([
  150. a.then(assert.fail, function(e) {
  151. assert.strictEqual(err, e);
  152. }),
  153. b.then(assert.fail, function(e) {
  154. assert.strictEqual(err, e);
  155. })
  156. ]);
  157. }
  158. if (Number(process.version[1]) >= 8)
  159. {
  160. const coreUtil = require('util');
  161. assert.strictEqual(coreUtil.promisify.custom, promisify.custom);
  162. }