1 | function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }
|
---|
2 | function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
|
---|
3 | function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
|
---|
4 | function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); }
|
---|
5 | function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }
|
---|
6 | function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
|
---|
7 | import { bind, last, map, curryN } from 'ramda';
|
---|
8 | import allP from './allP';
|
---|
9 | import lengthEq from './lengthEq';
|
---|
10 | import lengthGte from './lengthGte';
|
---|
11 | import rejectP from './rejectP';
|
---|
12 | import resolveP from './resolveP';
|
---|
13 |
|
---|
14 | /**
|
---|
15 | * Returns a promise that is fulfilled by the last given promise to be fulfilled,
|
---|
16 | * or rejected with an array of rejection reasons if all of the given promises are rejected.
|
---|
17 | *
|
---|
18 | * @func lastP
|
---|
19 | * @memberOf RA
|
---|
20 | * @category Function
|
---|
21 | * @since {@link https://char0n.github.io/ramda-adjunct/2.23.0|v2.23.0}
|
---|
22 | * @sig [Promise a] -> Promise a
|
---|
23 | * @param {Iterable.<*>} iterable An iterable object such as an Array or String
|
---|
24 | * @return {Promise} A promise that is fulfilled by the last given promise to be fulfilled, or rejected with an array of rejection reasons if all of the given promises are rejected.
|
---|
25 | * @see {@link RA.anyP|anyP}
|
---|
26 | * @example
|
---|
27 | *
|
---|
28 | * const delayP = timeout => new Promise(resolve => setTimeout(() => resolve(timeout), timeout));
|
---|
29 | * delayP.reject = timeout => new Promise((resolve, reject) => setTimeout(() => reject(timeout), timeout));
|
---|
30 | * RA.lastP([
|
---|
31 | * 1,
|
---|
32 | * delayP(10),
|
---|
33 | * delayP(100),
|
---|
34 | * delayP.reject(1000),
|
---|
35 | * ]); //=> Promise(100)
|
---|
36 | */
|
---|
37 | var lastP = curryN(1, function (iterable) {
|
---|
38 | var fulfilled = [];
|
---|
39 | var rejected = [];
|
---|
40 | var onFulfill = bind(fulfilled.push, fulfilled);
|
---|
41 | var onReject = bind(rejected.push, rejected);
|
---|
42 | var listOfPromises = map(function (p) {
|
---|
43 | return resolveP(p).then(onFulfill)["catch"](onReject);
|
---|
44 | }, _toConsumableArray(iterable));
|
---|
45 | return allP(listOfPromises).then(function () {
|
---|
46 | if (lengthEq(0, fulfilled) && lengthEq(0, rejected)) {
|
---|
47 | return undefined;
|
---|
48 | }
|
---|
49 | if (lengthGte(1, fulfilled)) {
|
---|
50 | return last(fulfilled);
|
---|
51 | }
|
---|
52 | return rejectP(rejected);
|
---|
53 | });
|
---|
54 | });
|
---|
55 | export default lastP; |
---|