source: node_modules/ramda-adjunct/es/lastP.js

main
Last change on this file was d24f17c, checked in by Aleksandar Panovski <apano77@…>, 15 months ago

Initial commit

  • Property mode set to 100644
File size: 2.9 KB
Line 
1function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }
2function _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."); }
3function _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); }
4function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); }
5function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }
6function _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; }
7import { bind, last, map, curryN } from 'ramda';
8import allP from './allP';
9import lengthEq from './lengthEq';
10import lengthGte from './lengthGte';
11import rejectP from './rejectP';
12import 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 */
37var 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});
55export default lastP;
Note: See TracBrowser for help on using the repository browser.