-
Notifications
You must be signed in to change notification settings - Fork 0
/
jobs.js
67 lines (54 loc) · 1.39 KB
/
jobs.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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
const axios = require('axios');
const _BASE_URL = 'https://hacker-news.firebaseio.com/v0/';
const JOB_URL = _BASE_URL + '/item/';
var JOBS = new Array();
var fetchJob = (urls) => {
return new Promise( (resolve, reject) => {
axios.all(urls).
then((jobs) => {
jobs.forEach((job) => {
JOBS.push(job['data']);
});
resolve();
}).
catch((error) => {
console.log(error);
reject('Server error');
});
})
};
var createJobRequest = (id) => {
var url = JOB_URL + id + '.json';
return axios.get(url, {
params: {
print: 'pretty'
}
});
};
var fetchJobIDs = () => {
return new Promise ((resolve, reject) => {
var jobIDs;
axios.get(_BASE_URL + 'jobstories.json', {
params: {
print: 'pretty'
}
}).
then((response) => {
var jobs_urls = new Array();
jobIDs = response['data'];
for (var i = 0; i < jobIDs.length; i++) {
jobs_urls.push(createJobRequest(jobIDs[i]));
}
fetchJob(jobs_urls).then(() => {
resolve({JOBS});
});
}).
catch((error) => {
console.log(error);
reject('Server error');
});
});
};
module.exports = {
fetchJobIDs
}