-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpromises.js
33 lines (28 loc) · 1.14 KB
/
promises.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
// A promise exists in 3 possible states
// unresolved, resolved, and rejected
// By default, a promise exists in the unresolved state (waiting for something to finish)
// resolved means something finished and it all went ok
// rejected means something finished and something went bad
//
// resolved ... then
// rejected ... catch
// I want to create a new promise
promise = new Promise((resolve, reject) => {
// let's simulate a long running process
setTimeout(() => {
resolve();
}, 3000);
// or
// reject(); This will turn the promise into the rejected state
});
promise
.then(() => console.log('finally finished'))
.then(() => console.log('I was also ran!!!'))
.catch(() => console.log('uh oh!!'));
//fetch helper
url = "https://jsonplaceholder.typicode.com/posts/";
fetch(url)
// .then(data => console.log(data)) //with fetch, this does not actually contain our data. This is an object that represents the response that we got back from the server
.then(response => response.json()) // gets our data
.then(data => console.log(data)) //now we can see our data gotten above
.catch(error => console.log(error));