-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmultiple-promises-fetch-with-ramda-sort.js
37 lines (27 loc) · 1.18 KB
/
multiple-promises-fetch-with-ramda-sort.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
const fetch = require("node-fetch"); // Tested on Node side
const R = require("ramda");
//Setup Ramda help functions
const sortByProp = (propName) => R.sortBy(R.prop(propName));
const sortByPropDesc = (propName) => R.compose(R.reverse, R.sortBy(R.prop(propName)));
const arrCombinedResult = [];
const urls = Array(25);
urls.fill('https://randomuser.me/api/') // using user random emulation
const fetchOneUrl = url => fetch(url)
.then(function(response) {
var s = response.json(); // json retuns a promise, use then to read the result
return s;
})
.then(function(myJson) {
//const sortedData = sortByProp('gender')(myJson.results) //If we need inside sort
arrCombinedResult.push(myJson.results[0]) //[0] to make this array of objects - by default iti is array of array of object
//console.log(sortedData);
})
Promise
.all(urls.map(fetchOneUrl))
.then(() => {
//const sortedData = R.sortBy(R.prop(['email']))(arrCombinedResult); //sort by first level prop
const sortedData = R.sortBy(R.path(['login','username']))(arrCombinedResult); //sort by nested level
console.log(sortedData);
}).catch((error)=> {
console.log('errorFromMyPromise: ' + error);
})