@util.js/promises 0.41.6 → 0.41.7
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 +440 -0
- package/package.json +5 -1
- package/doc/README-examples.js +0 -203
- package/lib/Promises.js +0 -411
- package/test/PromisesTest.js +0 -477
package/dist/Promises.js
ADDED
|
@@ -0,0 +1,440 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* JavaScript utility methods for [promises]{@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise}
|
|
5
|
+
* @public
|
|
6
|
+
* @class
|
|
7
|
+
*/
|
|
8
|
+
var _interopRequireDefault = require("@babel/runtime-corejs3/helpers/interopRequireDefault");
|
|
9
|
+
var _promise = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/promise"));
|
|
10
|
+
var _slice = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/instance/slice"));
|
|
11
|
+
var _from = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/array/from"));
|
|
12
|
+
var _concat = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/instance/concat"));
|
|
13
|
+
var _weakMap = _interopRequireDefault(require("@babel/runtime-corejs3/core-js-stable/weak-map"));
|
|
14
|
+
var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime-corejs3/helpers/classCallCheck"));
|
|
15
|
+
var _createClass2 = _interopRequireDefault(require("@babel/runtime-corejs3/helpers/createClass"));
|
|
16
|
+
var Promises = /*#__PURE__*/function () {
|
|
17
|
+
function Promises() {
|
|
18
|
+
(0, _classCallCheck2["default"])(this, Promises);
|
|
19
|
+
}
|
|
20
|
+
return (0, _createClass2["default"])(Promises, [{
|
|
21
|
+
key: "all",
|
|
22
|
+
value:
|
|
23
|
+
/**
|
|
24
|
+
* Returns a single Promise that resolves when all of the promises in the iterable argument have resolved or when the iterable argument contains no promises.
|
|
25
|
+
*
|
|
26
|
+
* It rejects with the reason of the first promise that rejects.
|
|
27
|
+
*
|
|
28
|
+
* See MDN's documentation about [all]{@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/all}.
|
|
29
|
+
*
|
|
30
|
+
* There is one minor difference between Promise#all and this method. Promise#all only takes one argument that must be iterable. This method, Promises#all, behaves the same as Promise#all when given one argument; when given multiple arguments, [arguments]{@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/arguments} becomes the iterable.
|
|
31
|
+
*
|
|
32
|
+
* @example
|
|
33
|
+
* const promises = require("@util.js/promises");
|
|
34
|
+
* const promise1 = Promise.resolve(3);
|
|
35
|
+
* const promise2 = 42;
|
|
36
|
+
* const promise3 = new Promise((resolve, reject) => {
|
|
37
|
+
* setTimeout(resolve, 100, "foo");
|
|
38
|
+
* });
|
|
39
|
+
* promises.all(promise1, promise2, promise3).then(values => {
|
|
40
|
+
* console.log(values);
|
|
41
|
+
* });
|
|
42
|
+
* // expected output: Array [3, 42, "foo"]
|
|
43
|
+
*
|
|
44
|
+
* @param {iterable|...*} iterable An iterable object such as an Array or String; if you give move than one argument to this method, [arguments]{@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/arguments} becomes the iterable
|
|
45
|
+
* @return {Promise} (A) An already resolved Promise if the iterable passed is empty.
|
|
46
|
+
* (B) An asynchronously resolved Promise if the iterable passed contains no promises.
|
|
47
|
+
* (C) A pending Promise in all other cases; this returned promise is then resolved/rejected asynchronously (as soon as the stack is empty) when all the promises in the given iterable have resolved, or if any of the promises reject; returned values will be in order of the Promises passed, regardless of completion order.
|
|
48
|
+
* @public
|
|
49
|
+
* @instance
|
|
50
|
+
* @function
|
|
51
|
+
*/
|
|
52
|
+
function all() {
|
|
53
|
+
if (arguments.length > 1) return _promise["default"].all(arguments);
|
|
54
|
+
return _promise["default"].all.apply(_promise["default"], arguments);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
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.
|
|
59
|
+
*
|
|
60
|
+
* Use this function if you are wrapping an existing function that only takes a callback but would like that function to handle both callbacks and Promises.
|
|
61
|
+
*
|
|
62
|
+
* It is also useful in writing new functions where you would like to support both callbacks and Promises.
|
|
63
|
+
*
|
|
64
|
+
* This function adapts functions that accept a callback. {@link Promises#applyPromise} adapts functions that return a Promise.
|
|
65
|
+
*
|
|
66
|
+
* This function is similar to Function#apply. It accepts a single array of arguments.
|
|
67
|
+
*
|
|
68
|
+
* Function#call and {@link Promises#callCallback} accept an argument list.
|
|
69
|
+
*
|
|
70
|
+
* When trying to remember the difference between #apply and #call, think, "#apply accepts an array. Both array and #apply start with a."
|
|
71
|
+
*
|
|
72
|
+
* @example
|
|
73
|
+
* // Wrap a function that only accepts a callback.
|
|
74
|
+
* const promises = require("@util.js/promises");
|
|
75
|
+
* const stream = require("stream");
|
|
76
|
+
* const streams = require("@util.js/node-streams");
|
|
77
|
+
* // stream#finished only takes a callback.
|
|
78
|
+
* // Wrap stream#finished so that it handles both callbacks and Promises.
|
|
79
|
+
* function finished() {
|
|
80
|
+
* return promises.applyCallback(stream, stream.finished, arguments);
|
|
81
|
+
* }
|
|
82
|
+
* const readableToCallback = streams.fromString("Call back, Hypnotoad!");
|
|
83
|
+
* finished(readableToCallback, () => console.log("Finished with a callback"));
|
|
84
|
+
* const readableToPromise = streams.fromString("Promise me, Hypnotoad!");
|
|
85
|
+
* finished(readableToPromise).then(() => console.log("Finished as promised"));
|
|
86
|
+
*
|
|
87
|
+
* @example
|
|
88
|
+
* // Write a function that supports both callbacks and Promises.
|
|
89
|
+
* const promises = require("@util.js/promises");
|
|
90
|
+
* function notify(message, who, callback) {
|
|
91
|
+
* if (!callback) return promises.applyCallback(null, notify, arguments);
|
|
92
|
+
* callback(null, `${message}, ${who}!`);
|
|
93
|
+
* }
|
|
94
|
+
* notify("Call back", "Hypnotoad", (error, message) => console.log(message));
|
|
95
|
+
* notify("Promise me", "Hypnotoad").then(console.log);
|
|
96
|
+
*
|
|
97
|
+
* @param {Object} object Value to use as this when executing functionOnObjectWithCallback (can be null)
|
|
98
|
+
* @param {Function} functionOnObjectWithCallback A function that takes a callback as its last argument
|
|
99
|
+
* @param args An array-like object containing the arguments to pass to functionOnObjectWithCallback; this is usually just [arguments]{@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/arguments}
|
|
100
|
+
* @return {undefined|Promise} undefined if the last element of args is a callback or a Promise if args does not contain a callback
|
|
101
|
+
* @throws {TypeError} If functionOnObjectWithCallback is not a function or if args is null or undefined
|
|
102
|
+
* @public
|
|
103
|
+
* @instance
|
|
104
|
+
* @function
|
|
105
|
+
*/
|
|
106
|
+
}, {
|
|
107
|
+
key: "applyCallback",
|
|
108
|
+
value: function applyCallback(object, functionOnObjectWithCallback, args) {
|
|
109
|
+
if (hasCallback(args)) return functionOnObjectWithCallback.apply(object, args);
|
|
110
|
+
return this.promisify(functionOnObjectWithCallback).apply(object, args);
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
/**
|
|
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.
|
|
115
|
+
*
|
|
116
|
+
* Use this function if you are wrapping an existing function that only returns a Promise but would like that function to handle both callbacks and Promises.
|
|
117
|
+
*
|
|
118
|
+
* It is also useful in writing new functions where you would like to support both callbacks and Promises.
|
|
119
|
+
*
|
|
120
|
+
* This function adapts functions that return a Promise. {@link Promises#applyCallback} adapts functions that accept a callback.
|
|
121
|
+
*
|
|
122
|
+
* This function is similar to Function#apply. It accepts a single array of arguments.
|
|
123
|
+
*
|
|
124
|
+
* Function#call and {@link Promises#callPromise} accept an argument list.
|
|
125
|
+
*
|
|
126
|
+
* When trying to remember the difference between #apply and #call, think, "#apply accepts an array. Both array and #apply start with a."
|
|
127
|
+
*
|
|
128
|
+
* @example
|
|
129
|
+
* // Wrap a function that only returns a Promise.
|
|
130
|
+
* const promises = require("@util.js/promises");
|
|
131
|
+
* function notifyPromise(message, who) {
|
|
132
|
+
* return promises.resolve(`${message}, ${who}!`);
|
|
133
|
+
* }
|
|
134
|
+
* // #notifyPromise only returns a Promise.
|
|
135
|
+
* // Wrap #notifyPromise so that it handles both callbacks and Promises.
|
|
136
|
+
* function notify(message, who, callback) {
|
|
137
|
+
* return promises.applyPromise(null, notifyPromise, arguments);
|
|
138
|
+
* }
|
|
139
|
+
* notify("Call back", "Hypnotoad", (error, message) => console.log(message));
|
|
140
|
+
* notify("Promise me", "Hypnotoad").then(console.log);
|
|
141
|
+
*
|
|
142
|
+
* @example
|
|
143
|
+
* // Write a function that supports both callbacks and Promises.
|
|
144
|
+
* const promises = require("@util.js/promises");
|
|
145
|
+
* function notify(message, who, callback) {
|
|
146
|
+
* if (callback) return promises.applyPromise(null, notify, arguments);
|
|
147
|
+
* return promises.resolve(`${message}, ${who}!`);
|
|
148
|
+
* }
|
|
149
|
+
* notify("Call back", "Hypnotoad", (error, message) => console.log(message));
|
|
150
|
+
* notify("Promise me", "Hypnotoad").then(console.log);
|
|
151
|
+
*
|
|
152
|
+
* @param {Object} object Value to use as this when executing promiseFunctionOnObject (can be null)
|
|
153
|
+
* @param {Function} promiseFunctionOnObject A function that returns a Promise
|
|
154
|
+
* @param args An array-like object containing the arguments to pass to promiseFunctionOnObject; this is usually just [arguments]{@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/arguments}
|
|
155
|
+
* @return {undefined|Promise} undefined if the last element of args is a callback or a Promise if args does not contain a callback
|
|
156
|
+
* @throws {TypeError} If promiseFunctionOnObject is not a function or if args is null or undefined
|
|
157
|
+
* @public
|
|
158
|
+
* @instance
|
|
159
|
+
* @function
|
|
160
|
+
*/
|
|
161
|
+
}, {
|
|
162
|
+
key: "applyPromise",
|
|
163
|
+
value: function applyPromise(object, promiseFunctionOnObject, args) {
|
|
164
|
+
if (hasCallback(args)) return this.callbackify(promiseFunctionOnObject).apply(object, args);
|
|
165
|
+
return promiseFunctionOnObject.apply(object, args);
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
/**
|
|
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.
|
|
170
|
+
*
|
|
171
|
+
* Use {@link Promises#callPromise} if you would like to callbackify a method and call it in one line.
|
|
172
|
+
*
|
|
173
|
+
* @example
|
|
174
|
+
* const promises = require("@util.js/promises");
|
|
175
|
+
* function notifyPromise(message, who) {
|
|
176
|
+
* return promises.resolve(`${message}, ${who}!`);
|
|
177
|
+
* }
|
|
178
|
+
* // #notifyPromise only returns a Promise.
|
|
179
|
+
* // Wrap #notifyPromise so that it accepts a callback.
|
|
180
|
+
* const notify = promises.callbackify(notifyPromise);
|
|
181
|
+
* notify("Call back", "Hypnotoad", (error, message) => console.log(message));
|
|
182
|
+
*
|
|
183
|
+
* @param {Function} promiseFunction A function that returns a Promise
|
|
184
|
+
* @return {Function} A function that accepts a callback
|
|
185
|
+
* @throws {TypeError} If promiseFunction is not a function
|
|
186
|
+
* @public
|
|
187
|
+
* @instance
|
|
188
|
+
* @function
|
|
189
|
+
*/
|
|
190
|
+
}, {
|
|
191
|
+
key: "callbackify",
|
|
192
|
+
value: function callbackify(promiseFunction) {
|
|
193
|
+
var returnValue = callbackifyCache().get(promiseFunction);
|
|
194
|
+
if (returnValue) return returnValue;
|
|
195
|
+
if (typeof promiseFunction !== "function") throw new TypeError("We expected promiseFunction to be function, but it was ".concat(promiseFunction, "."));
|
|
196
|
+
returnValue = function returnValue() {
|
|
197
|
+
var _context;
|
|
198
|
+
for (var _len = arguments.length, args = new Array(_len), _key = 0; _key < _len; _key++) {
|
|
199
|
+
args[_key] = arguments[_key];
|
|
200
|
+
}
|
|
201
|
+
var callback = arguments[args.length - 1];
|
|
202
|
+
if (typeof callback !== "function") throw new TypeError("We expected callback to be a function, but it was ".concat(callback, "."));
|
|
203
|
+
var argsWithoutCalback = (0, _slice["default"])(_context = (0, _from["default"])(args)).call(_context, 0, args.length - 1);
|
|
204
|
+
var promise;
|
|
205
|
+
try {
|
|
206
|
+
promise = promiseFunction.apply(this, argsWithoutCalback);
|
|
207
|
+
} catch (error) {
|
|
208
|
+
return callback(error);
|
|
209
|
+
}
|
|
210
|
+
if (!promise && !promise.then) throw new Error("We expected promiseFunction to return a Promise but instead got ".concat(promise, "."));
|
|
211
|
+
promise.then(function (result) {
|
|
212
|
+
return callback(null, result);
|
|
213
|
+
})["catch"](callback);
|
|
214
|
+
// It is possible for the callback to throw an error.
|
|
215
|
+
// This should be caught by
|
|
216
|
+
// process.on('unhandledRejection', error => { ... });
|
|
217
|
+
};
|
|
218
|
+
callbackifyCache().set(promiseFunction, returnValue);
|
|
219
|
+
return returnValue;
|
|
220
|
+
}
|
|
221
|
+
|
|
222
|
+
/**
|
|
223
|
+
* 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.
|
|
224
|
+
*
|
|
225
|
+
* This method just calls:
|
|
226
|
+
* <pre><code>
|
|
227
|
+
* this.applyCallback(object, functionOnObjectWithCallback, args)
|
|
228
|
+
* </code></pre>
|
|
229
|
+
*
|
|
230
|
+
* See {@link Promises#applyCallback}.
|
|
231
|
+
*
|
|
232
|
+
* @example
|
|
233
|
+
* const promises = require("@util.js/promises");
|
|
234
|
+
* function notifyCallback(message, who, callback) {
|
|
235
|
+
* callback(null, `${message}, ${who}!`);
|
|
236
|
+
* }
|
|
237
|
+
* // #notifyCallback only accpets a callback.
|
|
238
|
+
* // Force #notifyCallback to return a Promise.
|
|
239
|
+
* promises
|
|
240
|
+
* .callCallback(null, notifyCallback, "Promise me", "Hypnotoad")
|
|
241
|
+
* .then(console.log);
|
|
242
|
+
*
|
|
243
|
+
* @param {Object} object Value to use as this when executing functionOnObjectWithCallback (can be null)
|
|
244
|
+
* @param {Function} functionOnObjectWithCallback A function that takes a callback as its last argument
|
|
245
|
+
* @param {...*} [args] The arguments to pass to functionOnObjectWithCallback or its promisified version
|
|
246
|
+
* @return {undefined|Promise} undefined if the last element of args is a callback or a Promise if args does not contain a callback
|
|
247
|
+
* @throws {TypeError} If functionOnObjectWithCallback is not a function
|
|
248
|
+
* @public
|
|
249
|
+
* @instance
|
|
250
|
+
* @function
|
|
251
|
+
*/
|
|
252
|
+
}, {
|
|
253
|
+
key: "callCallback",
|
|
254
|
+
value: function callCallback(object, functionOnObjectWithCallback) {
|
|
255
|
+
for (var _len2 = arguments.length, args = new Array(_len2 > 2 ? _len2 - 2 : 0), _key2 = 2; _key2 < _len2; _key2++) {
|
|
256
|
+
args[_key2 - 2] = arguments[_key2];
|
|
257
|
+
}
|
|
258
|
+
return this.applyCallback(object, functionOnObjectWithCallback, args);
|
|
259
|
+
}
|
|
260
|
+
|
|
261
|
+
/**
|
|
262
|
+
* 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.
|
|
263
|
+
*
|
|
264
|
+
* This method just calls:
|
|
265
|
+
* <pre><code>
|
|
266
|
+
* this.applyCallback(object, functionOnObjectWithCallback, args)
|
|
267
|
+
* </code></pre>
|
|
268
|
+
*
|
|
269
|
+
* See {@link Promises#applyPromise}.
|
|
270
|
+
*
|
|
271
|
+
* @example
|
|
272
|
+
* const promises = require("@util.js/promises");
|
|
273
|
+
* function notifyPromise(message, who) {
|
|
274
|
+
* return promises.resolve(`${message}, ${who}!`);
|
|
275
|
+
* }
|
|
276
|
+
* // #notifyPromise only returns a Promise.
|
|
277
|
+
* // Force #notifyPromise to accept a callback.
|
|
278
|
+
* promises.callPromise(
|
|
279
|
+
* null,
|
|
280
|
+
* notifyPromise,
|
|
281
|
+
* "Call back",
|
|
282
|
+
* "Hypnotoad",
|
|
283
|
+
* (error, message) => console.log(message)
|
|
284
|
+
* );
|
|
285
|
+
*
|
|
286
|
+
* @param {Object} object Value to use as this when executing promiseFunctionOnObject (can be null)
|
|
287
|
+
* @param {Function} promiseFunctionOnObject A function that returns a Promise
|
|
288
|
+
* @param {...*} [args] The arguments to pass to promiseFunctionOnObject or its callbackified version
|
|
289
|
+
* @return {undefined|Promise} undefined if the last element of args is a callback or a Promise if args does not contain a callback
|
|
290
|
+
* @throws {TypeError} If promiseFunctionOnObject is not a function
|
|
291
|
+
* @public
|
|
292
|
+
* @instance
|
|
293
|
+
* @function
|
|
294
|
+
*/
|
|
295
|
+
}, {
|
|
296
|
+
key: "callPromise",
|
|
297
|
+
value: function callPromise(object, promiseFunctionOnObject) {
|
|
298
|
+
for (var _len3 = arguments.length, args = new Array(_len3 > 2 ? _len3 - 2 : 0), _key3 = 2; _key3 < _len3; _key3++) {
|
|
299
|
+
args[_key3 - 2] = arguments[_key3];
|
|
300
|
+
}
|
|
301
|
+
return this.applyPromise(object, promiseFunctionOnObject, args);
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
/**
|
|
305
|
+
* Wraps the given functionWithCallback such that calling the returned function returns a Promise that resolves if functionWithCallback succeeds and rejects if functionWithCallback errors.
|
|
306
|
+
*
|
|
307
|
+
* Use {@link Promises#callCallback} if you would like to promisify a method and call it in one line.
|
|
308
|
+
*
|
|
309
|
+
* @example
|
|
310
|
+
* const promises = require("@util.js/promises");
|
|
311
|
+
* const stream = require("stream");
|
|
312
|
+
* const streams = require("@util.js/node-streams");
|
|
313
|
+
* // stream#finished only takes a callback.
|
|
314
|
+
* // Let us wrap stream#finished so that it returns a Promise.
|
|
315
|
+
* const readable = streams.fromString("Promise me, Hypnotoad!");
|
|
316
|
+
* const finished = promises.promisify(stream.finished);
|
|
317
|
+
* finished(readable).then(() => console.log("Finished as promised"));
|
|
318
|
+
*
|
|
319
|
+
* @param {Function} functionWithCallback A function that takes a callback as its last argument
|
|
320
|
+
* @return {Function} A function that returns a Promise
|
|
321
|
+
* @throws {TypeError} If functionWithCallback is not a function
|
|
322
|
+
* @public
|
|
323
|
+
* @instance
|
|
324
|
+
* @function
|
|
325
|
+
*/
|
|
326
|
+
}, {
|
|
327
|
+
key: "promisify",
|
|
328
|
+
value: function promisify(functionWithCallback) {
|
|
329
|
+
var returnValue = promisifyCache().get(functionWithCallback);
|
|
330
|
+
if (returnValue) return returnValue;
|
|
331
|
+
if (typeof functionWithCallback !== "function") throw new TypeError("We expected functionWithCallback to be a function, but it was ".concat(functionWithCallback, "."));
|
|
332
|
+
returnValue = function returnValue() {
|
|
333
|
+
for (var _len4 = arguments.length, args = new Array(_len4), _key4 = 0; _key4 < _len4; _key4++) {
|
|
334
|
+
args[_key4] = arguments[_key4];
|
|
335
|
+
}
|
|
336
|
+
var thiz = this;
|
|
337
|
+
return new _promise["default"](function (resolve, reject) {
|
|
338
|
+
try {
|
|
339
|
+
var _context2;
|
|
340
|
+
functionWithCallback.call.apply(functionWithCallback, (0, _concat["default"])(_context2 = [thiz]).call(_context2, args, [function (error) {
|
|
341
|
+
if (error) return reject(error);
|
|
342
|
+
for (var _len5 = arguments.length, values = new Array(_len5 > 1 ? _len5 - 1 : 0), _key5 = 1; _key5 < _len5; _key5++) {
|
|
343
|
+
values[_key5 - 1] = arguments[_key5];
|
|
344
|
+
}
|
|
345
|
+
if (values.length === 0) return resolve();
|
|
346
|
+
if (values.length === 1) return resolve(values[0]);
|
|
347
|
+
resolve(values);
|
|
348
|
+
}]));
|
|
349
|
+
} catch (err) {
|
|
350
|
+
reject(err);
|
|
351
|
+
}
|
|
352
|
+
});
|
|
353
|
+
};
|
|
354
|
+
promisifyCache().set(functionWithCallback, returnValue);
|
|
355
|
+
return returnValue;
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
/**
|
|
359
|
+
* 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.
|
|
360
|
+
*
|
|
361
|
+
* See MDN's documentation about [race]{@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/race}.
|
|
362
|
+
*
|
|
363
|
+
* There is one minor difference between Promise#race and this method. Promise#race only takes one argument that must be iterable. This method, Promises#race, behaves the same as Promise#race when given one argument; when given multiple arguments, [arguments]{@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/arguments} becomes the iterable.
|
|
364
|
+
*
|
|
365
|
+
* @example
|
|
366
|
+
* const promises = require("@util.js/promises");
|
|
367
|
+
* const promise1 = new Promise((resolve, reject) => {
|
|
368
|
+
* setTimeout(resolve, 500, "one");
|
|
369
|
+
* });
|
|
370
|
+
* const promise2 = new Promise((resolve, reject) => {
|
|
371
|
+
* setTimeout(resolve, 100, "two");
|
|
372
|
+
* });
|
|
373
|
+
* promises.race(promise1, promise2).then(value => {
|
|
374
|
+
* console.log(value);
|
|
375
|
+
* // Both resolve, but promise2 is faster
|
|
376
|
+
* });
|
|
377
|
+
* // expected output: "two"
|
|
378
|
+
*
|
|
379
|
+
* @param {iterable|...*} iterable An iterable object such as an Array or String; if you give move than one argument to this method, [arguments]{@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/arguments} becomes the iterable
|
|
380
|
+
* @return {Promise} A pending Promise that resolves or rejects asynchronically (as soon as the stack is empty) as soon as one of the promises in the given iterable resolves or rejects, adopting that first promise's value as its value
|
|
381
|
+
* @public
|
|
382
|
+
* @instance
|
|
383
|
+
* @function
|
|
384
|
+
*/
|
|
385
|
+
}, {
|
|
386
|
+
key: "race",
|
|
387
|
+
value: function race() {
|
|
388
|
+
if (arguments.length > 1) return _promise["default"].race(arguments);
|
|
389
|
+
return _promise["default"].race.apply(_promise["default"], arguments);
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
/**
|
|
393
|
+
* Returns a Promise object that is rejected with the given reason.
|
|
394
|
+
*
|
|
395
|
+
* See MDN's documentation about [reject]{@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/reject}.
|
|
396
|
+
*
|
|
397
|
+
* @param {*} reason Reason why this Promise rejected
|
|
398
|
+
* @return {Promise} A Promise that is rejected with the given reason
|
|
399
|
+
* @public
|
|
400
|
+
* @instance
|
|
401
|
+
* @function
|
|
402
|
+
*/
|
|
403
|
+
}, {
|
|
404
|
+
key: "reject",
|
|
405
|
+
value: function reject() {
|
|
406
|
+
// eslint-disable-next-line prefer-promise-reject-errors
|
|
407
|
+
return _promise["default"].reject.apply(_promise["default"], arguments);
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
/**
|
|
411
|
+
* Returns a Promise object that is resolved with the given value.
|
|
412
|
+
*
|
|
413
|
+
* If the value is a promise, that promise is returned; if the value is a thenable (i.e. has a "then" method), the returned promise will "follow" that thenable, adopting its eventual state; otherwise the returned promise will be fulfilled with the value.
|
|
414
|
+
*
|
|
415
|
+
* See MDN's documentation about [resolve]{@link https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/resolve}.
|
|
416
|
+
*
|
|
417
|
+
* @param {*} value Argument to be resolved by this Promise that can also be a Promise or a thenable to resolve
|
|
418
|
+
* @return {Promise} A Promise that is resolved with the given value, or the promise passed as value, if the value was a promise object
|
|
419
|
+
* @public
|
|
420
|
+
* @instance
|
|
421
|
+
* @function
|
|
422
|
+
*/
|
|
423
|
+
}, {
|
|
424
|
+
key: "resolve",
|
|
425
|
+
value: function resolve() {
|
|
426
|
+
return _promise["default"].resolve.apply(_promise["default"], arguments);
|
|
427
|
+
}
|
|
428
|
+
}]);
|
|
429
|
+
}();
|
|
430
|
+
module.exports = Promises;
|
|
431
|
+
var dependencies = {};
|
|
432
|
+
function callbackifyCache() {
|
|
433
|
+
return dependencies.callbackifyCache || (dependencies.callbackifyCache = new _weakMap["default"]());
|
|
434
|
+
}
|
|
435
|
+
function promisifyCache() {
|
|
436
|
+
return dependencies.promisifyCache || (dependencies.promisifyCache = new _weakMap["default"]());
|
|
437
|
+
}
|
|
438
|
+
function hasCallback(args) {
|
|
439
|
+
return args.length > 0 && typeof args[args.length - 1] === "function";
|
|
440
|
+
}
|
package/package.json
CHANGED
|
@@ -8,6 +8,10 @@
|
|
|
8
8
|
"work in https://npm.runkit.com."
|
|
9
9
|
],
|
|
10
10
|
"description": "JavaScript utility methods for promises",
|
|
11
|
+
"files": [
|
|
12
|
+
"dist",
|
|
13
|
+
"index.js"
|
|
14
|
+
],
|
|
11
15
|
"keywords": [
|
|
12
16
|
"promise",
|
|
13
17
|
"promises",
|
|
@@ -28,5 +32,5 @@
|
|
|
28
32
|
"type": "git",
|
|
29
33
|
"url": "https://github.com/creemama/utiljs/tree/master/packages/utiljs-promises"
|
|
30
34
|
},
|
|
31
|
-
"version": "0.41.
|
|
35
|
+
"version": "0.41.7"
|
|
32
36
|
}
|
package/doc/README-examples.js
DELETED
|
@@ -1,203 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
if (process.argv.length < 3) {
|
|
4
|
-
for (let i = 0; i < 10; i++) runExample(i);
|
|
5
|
-
} else {
|
|
6
|
-
runExample(parseInt(process.argv[2]));
|
|
7
|
-
}
|
|
8
|
-
|
|
9
|
-
function runExample(i) {
|
|
10
|
-
switch (i) {
|
|
11
|
-
case 0:
|
|
12
|
-
runExample0();
|
|
13
|
-
break;
|
|
14
|
-
case 1:
|
|
15
|
-
runExample1();
|
|
16
|
-
break;
|
|
17
|
-
case 2:
|
|
18
|
-
runExample2();
|
|
19
|
-
break;
|
|
20
|
-
case 3:
|
|
21
|
-
runExample3();
|
|
22
|
-
break;
|
|
23
|
-
case 4:
|
|
24
|
-
runExample4();
|
|
25
|
-
break;
|
|
26
|
-
case 5:
|
|
27
|
-
runExample5();
|
|
28
|
-
break;
|
|
29
|
-
case 6:
|
|
30
|
-
runExample6();
|
|
31
|
-
break;
|
|
32
|
-
case 7:
|
|
33
|
-
runExample7();
|
|
34
|
-
break;
|
|
35
|
-
case 8:
|
|
36
|
-
runExample8();
|
|
37
|
-
break;
|
|
38
|
-
case 9:
|
|
39
|
-
runExample9();
|
|
40
|
-
break;
|
|
41
|
-
}
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
function runExample0() {
|
|
45
|
-
const promises = require("..");
|
|
46
|
-
const promise1 = Promise.resolve(3);
|
|
47
|
-
const promise2 = 42;
|
|
48
|
-
const promise3 = new Promise((resolve, reject) => {
|
|
49
|
-
setTimeout(resolve, 100, "foo");
|
|
50
|
-
});
|
|
51
|
-
promises.all(promise1, promise2, promise3).then((values) => {
|
|
52
|
-
console.log(values);
|
|
53
|
-
});
|
|
54
|
-
// expected output: Array [3, 42, "foo"]
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
function runExample1() {
|
|
58
|
-
// Wrap a function that only accepts a callback.
|
|
59
|
-
const promises = require("..");
|
|
60
|
-
const stream = require("stream");
|
|
61
|
-
const streams = require("../../utiljs-streams");
|
|
62
|
-
// stream#finished only takes a callback.
|
|
63
|
-
// Wrap stream#finished so that it handles both callbacks and Promises.
|
|
64
|
-
function finished() {
|
|
65
|
-
return promises.applyCallback(stream, stream.finished, arguments);
|
|
66
|
-
}
|
|
67
|
-
const readableToCallback = streams.fromString("Call back, Hypnotoad!");
|
|
68
|
-
finished(readableToCallback, () => console.log("Finished with a callback"));
|
|
69
|
-
const readableToPromise = streams.fromString("Promise me, Hypnotoad!");
|
|
70
|
-
finished(readableToPromise).then(() => console.log("Finished as promised"));
|
|
71
|
-
}
|
|
72
|
-
|
|
73
|
-
function runExample2() {
|
|
74
|
-
// Write a function that supports both callbacks and Promises.
|
|
75
|
-
const promises = require("..");
|
|
76
|
-
function notify(message, who, callback) {
|
|
77
|
-
if (!callback) return promises.applyCallback(null, notify, arguments);
|
|
78
|
-
callback(null, `${message}, ${who}!`);
|
|
79
|
-
}
|
|
80
|
-
notify("Call back", "Hypnotoad", (error, message) => {
|
|
81
|
-
if (error) {
|
|
82
|
-
console.error(error);
|
|
83
|
-
return;
|
|
84
|
-
}
|
|
85
|
-
console.log(message);
|
|
86
|
-
});
|
|
87
|
-
notify("Promise me", "Hypnotoad").then(console.log);
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
function runExample3() {
|
|
91
|
-
// Wrap a function that only returns a Promise.
|
|
92
|
-
const promises = require("..");
|
|
93
|
-
function notifyPromise(message, who) {
|
|
94
|
-
return promises.resolve(`${message}, ${who}!`);
|
|
95
|
-
}
|
|
96
|
-
// #notifyPromise only returns a Promise.
|
|
97
|
-
// Wrap #notifyPromise so that it handles both callbacks and Promises.
|
|
98
|
-
function notify(message, who, callback) {
|
|
99
|
-
return promises.applyPromise(null, notifyPromise, arguments);
|
|
100
|
-
}
|
|
101
|
-
notify("Call back", "Hypnotoad", (error, message) => {
|
|
102
|
-
if (error) {
|
|
103
|
-
console.error(error);
|
|
104
|
-
return;
|
|
105
|
-
}
|
|
106
|
-
console.log(message);
|
|
107
|
-
});
|
|
108
|
-
notify("Promise me", "Hypnotoad").then(console.log);
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
function runExample4() {
|
|
112
|
-
// Write a function that supports both callbacks and Promises.
|
|
113
|
-
const promises = require("..");
|
|
114
|
-
function notify(message, who, callback) {
|
|
115
|
-
if (callback) return promises.applyPromise(null, notify, arguments);
|
|
116
|
-
return promises.resolve(`${message}, ${who}!`);
|
|
117
|
-
}
|
|
118
|
-
notify("Call back", "Hypnotoad", (error, message) => {
|
|
119
|
-
if (error) {
|
|
120
|
-
console.error(error);
|
|
121
|
-
return;
|
|
122
|
-
}
|
|
123
|
-
console.log(message);
|
|
124
|
-
});
|
|
125
|
-
notify("Promise me", "Hypnotoad").then(console.log);
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
function runExample5() {
|
|
129
|
-
const promises = require("..");
|
|
130
|
-
function notifyPromise(message, who) {
|
|
131
|
-
return promises.resolve(`${message}, ${who}!`);
|
|
132
|
-
}
|
|
133
|
-
// #notifyPromise only returns a Promise.
|
|
134
|
-
// Wrap #notifyPromise so that it accepts a callback.
|
|
135
|
-
const notify = promises.callbackify(notifyPromise);
|
|
136
|
-
notify("Call back", "Hypnotoad", (error, message) => {
|
|
137
|
-
if (error) {
|
|
138
|
-
console.error(error);
|
|
139
|
-
return;
|
|
140
|
-
}
|
|
141
|
-
console.log(message);
|
|
142
|
-
});
|
|
143
|
-
}
|
|
144
|
-
|
|
145
|
-
function runExample6() {
|
|
146
|
-
const promises = require("..");
|
|
147
|
-
function notifyCallback(message, who, callback) {
|
|
148
|
-
callback(null, `${message}, ${who}!`);
|
|
149
|
-
}
|
|
150
|
-
// #notifyCallback only accpets a callback.
|
|
151
|
-
// Force #notifyCallback to return a Promise.
|
|
152
|
-
promises
|
|
153
|
-
.callCallback(null, notifyCallback, "Promise me", "Hypnotoad")
|
|
154
|
-
.then(console.log);
|
|
155
|
-
}
|
|
156
|
-
|
|
157
|
-
function runExample7() {
|
|
158
|
-
const promises = require("..");
|
|
159
|
-
function notifyPromise(message, who) {
|
|
160
|
-
return promises.resolve(`${message}, ${who}!`);
|
|
161
|
-
}
|
|
162
|
-
// #notifyPromise only returns a Promise.
|
|
163
|
-
// Force #notifyPromise to accept a callback.
|
|
164
|
-
promises.callPromise(
|
|
165
|
-
null,
|
|
166
|
-
notifyPromise,
|
|
167
|
-
"Call back",
|
|
168
|
-
"Hypnotoad",
|
|
169
|
-
(error, message) => {
|
|
170
|
-
if (error) {
|
|
171
|
-
console.error(error);
|
|
172
|
-
return;
|
|
173
|
-
}
|
|
174
|
-
console.log(message);
|
|
175
|
-
},
|
|
176
|
-
);
|
|
177
|
-
}
|
|
178
|
-
|
|
179
|
-
function runExample8() {
|
|
180
|
-
const promises = require("..");
|
|
181
|
-
const stream = require("stream");
|
|
182
|
-
const streams = require("../../utiljs-streams");
|
|
183
|
-
// stream#finished only takes a callback.
|
|
184
|
-
// Let us wrap stream#finished so that it returns a Promise.
|
|
185
|
-
const readable = streams.fromString("Promise me, Hypnotoad!");
|
|
186
|
-
const finished = promises.promisify(stream.finished);
|
|
187
|
-
finished(readable).then(() => console.log("Finished as promised"));
|
|
188
|
-
}
|
|
189
|
-
|
|
190
|
-
function runExample9() {
|
|
191
|
-
const promises = require("..");
|
|
192
|
-
const promise1 = new Promise((resolve, reject) => {
|
|
193
|
-
setTimeout(resolve, 500, "one");
|
|
194
|
-
});
|
|
195
|
-
const promise2 = new Promise((resolve, reject) => {
|
|
196
|
-
setTimeout(resolve, 100, "two");
|
|
197
|
-
});
|
|
198
|
-
promises.race(promise1, promise2).then((value) => {
|
|
199
|
-
console.log(value);
|
|
200
|
-
// Both resolve, but promise2 is faster
|
|
201
|
-
});
|
|
202
|
-
// expected output: "two"
|
|
203
|
-
}
|