@util.js/promises 0.41.0 → 0.41.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/Promises.js CHANGED
@@ -1,31 +1,22 @@
1
1
  "use strict";
2
+
2
3
  /**
3
4
  * JavaScript utility methods for [promises]{@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise}
4
5
  * @public
5
6
  * @class
6
7
  */
7
-
8
8
  var _interopRequireDefault = require("@babel/runtime-corejs3/helpers/interopRequireDefault");
9
-
10
9
  var _promise = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/promise"));
11
-
12
10
  var _slice = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/instance/slice"));
13
-
14
11
  var _from = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/array/from"));
15
-
16
12
  var _concat = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/instance/concat"));
17
-
18
13
  var _weakMap = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/weak-map"));
19
-
20
14
  var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime-corejs3/helpers/classCallCheck"));
21
-
22
15
  var _createClass2 = _interopRequireDefault(require("@babel/runtime-corejs3/helpers/createClass"));
23
-
24
16
  var Promises = /*#__PURE__*/function () {
25
17
  function Promises() {
26
18
  (0, _classCallCheck2["default"])(this, Promises);
27
19
  }
28
-
29
20
  (0, _createClass2["default"])(Promises, [{
30
21
  key: "all",
31
22
  value:
@@ -62,6 +53,7 @@ var Promises = /*#__PURE__*/function () {
62
53
  if (arguments.length > 1) return _promise["default"].all(arguments);
63
54
  return _promise["default"].all.apply(_promise["default"], arguments);
64
55
  }
56
+
65
57
  /**
66
58
  * Calls the given functionOnObjectWithCallback with the given args that either (A) returns a Promise if args does not contain a callback or (B) notifies the callback if args contains a callback.
67
59
  *
@@ -111,13 +103,13 @@ var Promises = /*#__PURE__*/function () {
111
103
  * @instance
112
104
  * @function
113
105
  */
114
-
115
106
  }, {
116
107
  key: "applyCallback",
117
108
  value: function applyCallback(object, functionOnObjectWithCallback, args) {
118
109
  if (hasCallback(args)) return functionOnObjectWithCallback.apply(object, args);
119
110
  return this.promisify(functionOnObjectWithCallback).apply(object, args);
120
111
  }
112
+
121
113
  /**
122
114
  * Calls the given promiseFunctionOnObject with the given args that either (A) returns a Promise if args does not contain a callback or (B) notifies the callback if args contains a callback.
123
115
  *
@@ -166,13 +158,13 @@ var Promises = /*#__PURE__*/function () {
166
158
  * @instance
167
159
  * @function
168
160
  */
169
-
170
161
  }, {
171
162
  key: "applyPromise",
172
163
  value: function applyPromise(object, promiseFunctionOnObject, args) {
173
164
  if (hasCallback(args)) return this.callbackify(promiseFunctionOnObject).apply(object, args);
174
165
  return promiseFunctionOnObject.apply(object, args);
175
166
  }
167
+
176
168
  /**
177
169
  * Wraps the given promiseFunction such that calling the returned function with a callback notifies the callback with an error if promiseFunction rejects or the return value if promiseFunction resolves.
178
170
  *
@@ -195,36 +187,31 @@ var Promises = /*#__PURE__*/function () {
195
187
  * @instance
196
188
  * @function
197
189
  */
198
-
199
190
  }, {
200
191
  key: "callbackify",
201
192
  value: function callbackify(promiseFunction) {
202
193
  var returnValue = callbackifyCache().get(promiseFunction);
203
194
  if (returnValue) return returnValue;
204
195
  if (typeof promiseFunction !== "function") throw new TypeError("We expected promiseFunction to be function, but it was ".concat(promiseFunction, "."));
205
-
206
196
  returnValue = function returnValue() {
207
197
  var _context;
208
-
209
198
  for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
210
199
  args[_key] = arguments[_key];
211
200
  }
212
-
213
201
  var callback = arguments[args.length - 1];
214
202
  if (typeof callback !== "function") throw new TypeError("We expected callback to be a function, but it was ".concat(callback, "."));
215
203
  var argsWithoutCalback = (0, _slice["default"])(_context = (0, _from["default"])(args)).call(_context, 0, args.length - 1);
216
204
  var promise;
217
-
218
205
  try {
219
206
  promise = promiseFunction.apply(this, argsWithoutCalback);
220
207
  } catch (error) {
221
208
  return callback(error);
222
209
  }
223
-
224
210
  if (!promise && !promise.then) throw new Error("We expected promiseFunction to return a Promise but instead got ".concat(promise, "."));
225
211
  promise.then(function (result) {
226
212
  return callback(null, result);
227
- })["catch"](callback); // It is possible for the callback to throw an error.
213
+ })["catch"](callback);
214
+ // It is possible for the callback to throw an error.
228
215
  // This should be caught by
229
216
  // process.on('unhandledRejection', error => { ... });
230
217
  };
@@ -232,6 +219,7 @@ var Promises = /*#__PURE__*/function () {
232
219
  callbackifyCache().set(promiseFunction, returnValue);
233
220
  return returnValue;
234
221
  }
222
+
235
223
  /**
236
224
  * Calls the given functionOnObjectWithCallback with the given args that either (A) returns a Promise if args does not contain a callback or (B) notifies the callback if args contains a callback.
237
225
  *
@@ -262,16 +250,15 @@ var Promises = /*#__PURE__*/function () {
262
250
  * @instance
263
251
  * @function
264
252
  */
265
-
266
253
  }, {
267
254
  key: "callCallback",
268
255
  value: function callCallback(object, functionOnObjectWithCallback) {
269
256
  for (var _len2 = arguments.length, args = new Array(_len2 > 2 ? _len2 - 2 : 0), _key2 = 2; _key2 < _len2; _key2++) {
270
257
  args[_key2 - 2] = arguments[_key2];
271
258
  }
272
-
273
259
  return this.applyCallback(object, functionOnObjectWithCallback, args);
274
260
  }
261
+
275
262
  /**
276
263
  * Calls the given promiseFunctionOnObject with the given args that either (A) returns a Promise if args does not contain a callback or (B) notifies the callback if args contains a callback.
277
264
  *
@@ -306,16 +293,15 @@ var Promises = /*#__PURE__*/function () {
306
293
  * @instance
307
294
  * @function
308
295
  */
309
-
310
296
  }, {
311
297
  key: "callPromise",
312
298
  value: function callPromise(object, promiseFunctionOnObject) {
313
299
  for (var _len3 = arguments.length, args = new Array(_len3 > 2 ? _len3 - 2 : 0), _key3 = 2; _key3 < _len3; _key3++) {
314
300
  args[_key3 - 2] = arguments[_key3];
315
301
  }
316
-
317
302
  return this.applyPromise(object, promiseFunctionOnObject, args);
318
303
  }
304
+
319
305
  /**
320
306
  * Wraps the given functionWithCallback such that calling the returned function returns a Promise that resolves if functionWithCallback succeeds and rejects if functionWithCallback errors.
321
307
  *
@@ -338,33 +324,27 @@ var Promises = /*#__PURE__*/function () {
338
324
  * @instance
339
325
  * @function
340
326
  */
341
-
342
327
  }, {
343
328
  key: "promisify",
344
329
  value: function promisify(functionWithCallback) {
345
330
  var returnValue = promisifyCache().get(functionWithCallback);
346
331
  if (returnValue) return returnValue;
347
332
  if (typeof functionWithCallback !== "function") throw new TypeError("We expected functionWithCallback to be a function, but it was ".concat(functionWithCallback, "."));
348
-
349
333
  returnValue = function returnValue() {
350
334
  for (var _len4 = arguments.length, args = new Array(_len4), _key4 = 0; _key4 < _len4; _key4++) {
351
335
  args[_key4] = arguments[_key4];
352
336
  }
353
-
354
337
  var thiz = this;
355
338
  return new _promise["default"](function (resolve, reject) {
356
339
  try {
357
340
  var _context2;
358
-
359
341
  functionWithCallback.call.apply(functionWithCallback, (0, _concat["default"])(_context2 = [thiz]).call(_context2, args, [function (error) {
360
342
  if (error) return reject(error);
361
-
362
343
  for (var _len5 = arguments.length, values = new Array(_len5 > 1 ? _len5 - 1 : 0), _key5 = 1; _key5 < _len5; _key5++) {
363
344
  values[_key5 - 1] = arguments[_key5];
364
345
  }
365
-
366
- if (values.length == 0) return resolve();
367
- if (values.length == 1) return resolve(values[0]);
346
+ if (values.length === 0) return resolve();
347
+ if (values.length === 1) return resolve(values[0]);
368
348
  resolve(values);
369
349
  }]));
370
350
  } catch (err) {
@@ -372,10 +352,10 @@ var Promises = /*#__PURE__*/function () {
372
352
  }
373
353
  });
374
354
  };
375
-
376
355
  promisifyCache().set(functionWithCallback, returnValue);
377
356
  return returnValue;
378
357
  }
358
+
379
359
  /**
380
360
  * Returns a promise that resolves or rejects as soon as one of the promises in the iterable resolves or rejects, with the value or reason from that promise.
381
361
  *
@@ -403,13 +383,13 @@ var Promises = /*#__PURE__*/function () {
403
383
  * @instance
404
384
  * @function
405
385
  */
406
-
407
386
  }, {
408
387
  key: "race",
409
388
  value: function race() {
410
389
  if (arguments.length > 1) return _promise["default"].race(arguments);
411
390
  return _promise["default"].race.apply(_promise["default"], arguments);
412
391
  }
392
+
413
393
  /**
414
394
  * Returns a Promise object that is rejected with the given reason.
415
395
  *
@@ -421,12 +401,13 @@ var Promises = /*#__PURE__*/function () {
421
401
  * @instance
422
402
  * @function
423
403
  */
424
-
425
404
  }, {
426
405
  key: "reject",
427
406
  value: function reject() {
407
+ // eslint-disable-next-line prefer-promise-reject-errors
428
408
  return _promise["default"].reject.apply(_promise["default"], arguments);
429
409
  }
410
+
430
411
  /**
431
412
  * Returns a Promise object that is resolved with the given value.
432
413
  *
@@ -440,7 +421,6 @@ var Promises = /*#__PURE__*/function () {
440
421
  * @instance
441
422
  * @function
442
423
  */
443
-
444
424
  }, {
445
425
  key: "resolve",
446
426
  value: function resolve() {
@@ -449,18 +429,14 @@ var Promises = /*#__PURE__*/function () {
449
429
  }]);
450
430
  return Promises;
451
431
  }();
452
-
453
432
  module.exports = Promises;
454
433
  var dependencies = {};
455
-
456
434
  function callbackifyCache() {
457
- return dependencies["callbackifyCache"] || (dependencies["callbackifyCache"] = new _weakMap["default"]());
435
+ return dependencies.callbackifyCache || (dependencies.callbackifyCache = new _weakMap["default"]());
458
436
  }
459
-
460
437
  function promisifyCache() {
461
- return dependencies["promisifyCache"] || (dependencies["promisifyCache"] = new _weakMap["default"]());
438
+ return dependencies.promisifyCache || (dependencies.promisifyCache = new _weakMap["default"]());
462
439
  }
463
-
464
440
  function hasCallback(args) {
465
441
  return args.length > 0 && typeof args[args.length - 1] === "function";
466
442
  }
@@ -77,7 +77,13 @@ function runExample2() {
77
77
  if (!callback) return promises.applyCallback(null, notify, arguments);
78
78
  callback(null, `${message}, ${who}!`);
79
79
  }
80
- notify("Call back", "Hypnotoad", (error, message) => console.log(message));
80
+ notify("Call back", "Hypnotoad", (error, message) => {
81
+ if (error) {
82
+ console.error(error);
83
+ return;
84
+ }
85
+ console.log(message);
86
+ });
81
87
  notify("Promise me", "Hypnotoad").then(console.log);
82
88
  }
83
89
 
@@ -92,7 +98,13 @@ function runExample3() {
92
98
  function notify(message, who, callback) {
93
99
  return promises.applyPromise(null, notifyPromise, arguments);
94
100
  }
95
- notify("Call back", "Hypnotoad", (error, message) => console.log(message));
101
+ notify("Call back", "Hypnotoad", (error, message) => {
102
+ if (error) {
103
+ console.error(error);
104
+ return;
105
+ }
106
+ console.log(message);
107
+ });
96
108
  notify("Promise me", "Hypnotoad").then(console.log);
97
109
  }
98
110
 
@@ -103,7 +115,13 @@ function runExample4() {
103
115
  if (callback) return promises.applyPromise(null, notify, arguments);
104
116
  return promises.resolve(`${message}, ${who}!`);
105
117
  }
106
- notify("Call back", "Hypnotoad", (error, message) => console.log(message));
118
+ notify("Call back", "Hypnotoad", (error, message) => {
119
+ if (error) {
120
+ console.error(error);
121
+ return;
122
+ }
123
+ console.log(message);
124
+ });
107
125
  notify("Promise me", "Hypnotoad").then(console.log);
108
126
  }
109
127
 
@@ -115,7 +133,13 @@ function runExample5() {
115
133
  // #notifyPromise only returns a Promise.
116
134
  // Wrap #notifyPromise so that it accepts a callback.
117
135
  const notify = promises.callbackify(notifyPromise);
118
- notify("Call back", "Hypnotoad", (error, message) => console.log(message));
136
+ notify("Call back", "Hypnotoad", (error, message) => {
137
+ if (error) {
138
+ console.error(error);
139
+ return;
140
+ }
141
+ console.log(message);
142
+ });
119
143
  }
120
144
 
121
145
  function runExample6() {
@@ -142,7 +166,13 @@ function runExample7() {
142
166
  notifyPromise,
143
167
  "Call back",
144
168
  "Hypnotoad",
145
- (error, message) => console.log(message)
169
+ (error, message) => {
170
+ if (error) {
171
+ console.error(error);
172
+ return;
173
+ }
174
+ console.log(message);
175
+ }
146
176
  );
147
177
  }
148
178
 
package/index.js CHANGED
@@ -1,4 +1,5 @@
1
1
  "use strict";
2
2
 
3
+ // eslint-disable-next-line no-var
3
4
  var Promises = require("./dist/Promises");
4
5
  module.exports = new Promises();
package/lib/Promises.js CHANGED
@@ -311,8 +311,8 @@ class Promises {
311
311
  try {
312
312
  functionWithCallback.call(thiz, ...args, function (error, ...values) {
313
313
  if (error) return reject(error);
314
- if (values.length == 0) return resolve();
315
- if (values.length == 1) return resolve(values[0]);
314
+ if (values.length === 0) return resolve();
315
+ if (values.length === 1) return resolve(values[0]);
316
316
  resolve(values);
317
317
  });
318
318
  } catch (err) {
@@ -368,6 +368,7 @@ class Promises {
368
368
  * @function
369
369
  */
370
370
  reject() {
371
+ // eslint-disable-next-line prefer-promise-reject-errors
371
372
  return Promise.reject(...arguments);
372
373
  }
373
374
 
@@ -395,14 +396,13 @@ const dependencies = {};
395
396
 
396
397
  function callbackifyCache() {
397
398
  return (
398
- dependencies["callbackifyCache"] ||
399
- (dependencies["callbackifyCache"] = new WeakMap())
399
+ dependencies.callbackifyCache ||
400
+ (dependencies.callbackifyCache = new WeakMap())
400
401
  );
401
402
  }
402
403
  function promisifyCache() {
403
404
  return (
404
- dependencies["promisifyCache"] ||
405
- (dependencies["promisifyCache"] = new WeakMap())
405
+ dependencies.promisifyCache || (dependencies.promisifyCache = new WeakMap())
406
406
  );
407
407
  }
408
408
 
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "author": "Chris Topher <c@creemama.com>",
3
3
  "dependencies": {
4
- "@babel/runtime-corejs3": "7.14.0"
4
+ "@babel/runtime-corejs3": "7.20.1"
5
5
  },
6
6
  "dependencies-comments": [
7
7
  "@babel/runtime-corejs3 is required by files in the dist directory to ",
@@ -28,6 +28,6 @@
28
28
  "type": "git",
29
29
  "url": "https://github.com/creemama/utiljs/tree/master/packages/utiljs-promises"
30
30
  },
31
- "version": "0.41.0",
32
- "gitHead": "1ae0fda34e06ffbc546c5ba0025b0b59a0c7727d"
31
+ "version": "0.41.2",
32
+ "gitHead": "174b81ec0ead0221b9bb6617bc254b025bd20a5a"
33
33
  }
@@ -1,13 +1,13 @@
1
1
  "use strict";
2
2
 
3
- const { expect } = require("chai"),
4
- promises = require("..");
3
+ const { expect } = require("chai");
4
+ const promises = require("..");
5
5
 
6
6
  describe("Promises", function () {
7
7
  describe("#(apply|call)Callback(object, functionOnObjectWithCallback, args)", () => {
8
8
  function notify(message, who, callback) {
9
- if (message == "Throw error") throw new Error("Hypnotoad died.");
10
- if (message == "Return undefined") return;
9
+ if (message === "Throw error") throw new Error("Hypnotoad died.");
10
+ if (message === "Return undefined") return;
11
11
  callback(null, `${message}, ${who}!`);
12
12
  }
13
13
  it("should resolve a Promise", async function () {
@@ -64,8 +64,8 @@ describe("Promises", function () {
64
64
 
65
65
  describe("#(apply|call)Promise(object, promiseFunctionOnObject, args)", () => {
66
66
  function notify(message, who) {
67
- if (message == "Throw error") throw new Error("Hypnotoad died.");
68
- if (message == "Return undefined") return;
67
+ if (message === "Throw error") throw new Error("Hypnotoad died.");
68
+ if (message === "Return undefined") return;
69
69
  return promises.resolve(`${message}, ${who}!`);
70
70
  }
71
71
  it("should notify a callback", (callback) => {
@@ -75,6 +75,10 @@ describe("Promises", function () {
75
75
  "Call back",
76
76
  "Hypnotoad",
77
77
  (error, message) => {
78
+ if (error) {
79
+ callbck(error);
80
+ return;
81
+ }
78
82
  expect(message).to.eql("Call back, Hypnotoad!");
79
83
  b(callbck);
80
84
  },
@@ -87,6 +91,10 @@ describe("Promises", function () {
87
91
  "Call back",
88
92
  "Hypnotoad",
89
93
  (error, message) => {
94
+ if (error) {
95
+ callbck(error);
96
+ return;
97
+ }
90
98
  expect(message).to.eql("Call back, Hypnotoad!");
91
99
  c(callbck);
92
100
  }
@@ -103,6 +111,10 @@ describe("Promises", function () {
103
111
  "Call back",
104
112
  "Hypnotoad",
105
113
  (error, value) => {
114
+ if (error) {
115
+ callbck(error);
116
+ return;
117
+ }
106
118
  try {
107
119
  expect(value).to.eql("Call back, Hypnotoad!");
108
120
  callbck();
@@ -206,7 +218,8 @@ describe("Promises", function () {
206
218
  (error, value) => {
207
219
  callback(
208
220
  new Error(
209
- "We expected #callPromise to not call this callback."
221
+ "We expected #callPromise to not call this callback.",
222
+ { cause: error }
210
223
  )
211
224
  );
212
225
  }
@@ -354,7 +367,7 @@ describe("Promises", function () {
354
367
  }
355
368
  const a = promises.callbackify(promiseFunction);
356
369
  const b = promises.callbackify(promiseFunction);
357
- expect(a == b).to.be.ok;
370
+ expect(a === b).to.be.ok;
358
371
  });
359
372
  it("should throw an error if promiseFunction is not a function", () => {
360
373
  expect(() => promises.callbackify()).to.throw(TypeError);
@@ -393,7 +406,7 @@ describe("Promises", function () {
393
406
  }
394
407
  const a = promises.promisify(functionWithCallback);
395
408
  const b = promises.promisify(functionWithCallback);
396
- expect(a == b).to.be.ok;
409
+ expect(a === b).to.be.ok;
397
410
  });
398
411
  it("should reject if functionWithCallback is not a function", () => {
399
412
  expect(() => promises.promisify()).to.throw(TypeError);