source: node_modules/ramda-adjunct/src/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: 1.7 KB
RevLine 
[d24f17c]1import { bind, last, map, curryN } from 'ramda';
2
3import allP from './allP';
4import lengthEq from './lengthEq';
5import lengthGte from './lengthGte';
6import rejectP from './rejectP';
7import resolveP from './resolveP';
8
9/**
10 * Returns a promise that is fulfilled by the last given promise to be fulfilled,
11 * or rejected with an array of rejection reasons if all of the given promises are rejected.
12 *
13 * @func lastP
14 * @memberOf RA
15 * @category Function
16 * @since {@link https://char0n.github.io/ramda-adjunct/2.23.0|v2.23.0}
17 * @sig [Promise a] -> Promise a
18 * @param {Iterable.<*>} iterable An iterable object such as an Array or String
19 * @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.
20 * @see {@link RA.anyP|anyP}
21 * @example
22 *
23 * const delayP = timeout => new Promise(resolve => setTimeout(() => resolve(timeout), timeout));
24 * delayP.reject = timeout => new Promise((resolve, reject) => setTimeout(() => reject(timeout), timeout));
25 * RA.lastP([
26 * 1,
27 * delayP(10),
28 * delayP(100),
29 * delayP.reject(1000),
30 * ]); //=> Promise(100)
31 */
32const lastP = curryN(1, (iterable) => {
33 const fulfilled = [];
34 const rejected = [];
35 const onFulfill = bind(fulfilled.push, fulfilled);
36 const onReject = bind(rejected.push, rejected);
37
38 const listOfPromises = map(
39 (p) => resolveP(p).then(onFulfill).catch(onReject),
40 [...iterable]
41 );
42
43 return allP(listOfPromises).then(() => {
44 if (lengthEq(0, fulfilled) && lengthEq(0, rejected)) {
45 return undefined;
46 }
47 if (lengthGte(1, fulfilled)) {
48 return last(fulfilled);
49 }
50 return rejectP(rejected);
51 });
52});
53
54export default lastP;
Note: See TracBrowser for help on using the repository browser.