@util.js/promises 0.41.1 → 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 +12 -37
- package/package.json +3 -3
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);
|
|
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,31 +324,25 @@ 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
346
|
if (values.length === 0) return resolve();
|
|
367
347
|
if (values.length === 1) return resolve(values[0]);
|
|
368
348
|
resolve(values);
|
|
@@ -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,13 +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() {
|
|
428
407
|
// eslint-disable-next-line prefer-promise-reject-errors
|
|
429
408
|
return _promise["default"].reject.apply(_promise["default"], arguments);
|
|
430
409
|
}
|
|
410
|
+
|
|
431
411
|
/**
|
|
432
412
|
* Returns a Promise object that is resolved with the given value.
|
|
433
413
|
*
|
|
@@ -441,7 +421,6 @@ var Promises = /*#__PURE__*/function () {
|
|
|
441
421
|
* @instance
|
|
442
422
|
* @function
|
|
443
423
|
*/
|
|
444
|
-
|
|
445
424
|
}, {
|
|
446
425
|
key: "resolve",
|
|
447
426
|
value: function resolve() {
|
|
@@ -450,18 +429,14 @@ var Promises = /*#__PURE__*/function () {
|
|
|
450
429
|
}]);
|
|
451
430
|
return Promises;
|
|
452
431
|
}();
|
|
453
|
-
|
|
454
432
|
module.exports = Promises;
|
|
455
433
|
var dependencies = {};
|
|
456
|
-
|
|
457
434
|
function callbackifyCache() {
|
|
458
435
|
return dependencies.callbackifyCache || (dependencies.callbackifyCache = new _weakMap["default"]());
|
|
459
436
|
}
|
|
460
|
-
|
|
461
437
|
function promisifyCache() {
|
|
462
438
|
return dependencies.promisifyCache || (dependencies.promisifyCache = new _weakMap["default"]());
|
|
463
439
|
}
|
|
464
|
-
|
|
465
440
|
function hasCallback(args) {
|
|
466
441
|
return args.length > 0 && typeof args[args.length - 1] === "function";
|
|
467
442
|
}
|
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.
|
|
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.
|
|
32
|
-
"gitHead": "
|
|
31
|
+
"version": "0.41.2",
|
|
32
|
+
"gitHead": "174b81ec0ead0221b9bb6617bc254b025bd20a5a"
|
|
33
33
|
}
|