-
Notifications
You must be signed in to change notification settings - Fork 1
/
demo4-node-fetch-example.js
68 lines (62 loc) · 2.21 KB
/
demo4-node-fetch-example.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 fetch = require('node-fetch');
const setTimeToNewYork = require('./nytime');
/*
* This is the second method used for retrieving the next meetup. It used a Promise.
* The function returns by default.
*/
const getNextMeetup = () =>
fetch( 'https://api.meetup.com/2/events?&sign=true&group_id=10250862&page=20&key=' + process.env.meetupapi_key)
.then(response => { return response.json();})
.then(json => {
const meetingArray = json.results;
setTimeToNewYork(meetingArray);
return meetingArray[0];
})
/*
* This implementation uses a function definition and does not return by default.
* This implementation shows everything that goes on under the hood.
* A promise that succeeds is in the resolve state, when a failure occours it switches to the reject state.
function verboseGetNextMeetup() {
return fetch('https://api.meetup.com/2/events?&sign=true&group_id=10250862&page=20&key=' + process.env.meetupapi_key)
.then(
response => { return response.json();},
err => { throw err }
)
.then(
json => {
const meetingArray = json.results;
setTimeToNewYork(meetingArray);
return meetingArray[0]; },
err => { throw err }
);
}
* '.then' accepts two functions as parameters, resolve fn and reject fn
* commonly '.then' is used with one parameter, only to handle resolve,
* and using .catch to handle reject. However, '.catch' can swallow errors
* that could occour from a dispatched action, setState triggering render function, etc.
* see demo7-promise-react-example.js
*/
getNextMeetup().then(
results => { console.log(results.name); },
err => { console.error('Your specific error message', err); }
)
/*
* to compare with getNextMeetup above.
* see demo7-promise-react-example.js
getNextMeetup()
.then(results => {
console.log(results.name);
})
.catch(err => {
console.error('Your specific error message', err);
});
*/
// to compare with getNextMeetup above.
async function asyncGetNextMeetup(){
response = await fetch(
'https://api.meetup.com/2/events?&sign=true&group_id=10250862&page=20&key=' + process.env.meetupapi_key
)
const meetingArray = response.json().results
setTimeToNewYork(meetingArray);
return meetingArray[0];
}