-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathpromises.js
More file actions
37 lines (33 loc) · 1020 Bytes
/
promises.js
File metadata and controls
37 lines (33 loc) · 1020 Bytes
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
// DEFINE PROMISE
// const noMondays = new Promise((resolve, reject) => {
// // IF TODAY IS NOT MONDAY - SUCCESS
// if (new Date().getDay() !== 2) {
// resolve("Good, it's not Tuesday!");
// } else {
// reject("NO MORE MONDAYS! THREE DAY WEEKENDS FOREVER");
// }
// });
// CHECK PROMISE
// noMondays
// .then(res => console.log("THEN: ", res))
// .catch(err => console.log("CATCH: ", err))
// thenable
const axios = (url) => new Promise((resolve, reject) => {
console.log(`YOU ARE MAKING A REQUEST TO ${url}`)
let randomNumber = Math.floor(Math.random() * 10)
if(randomNumber === 0){
reject({
'status' : 'error',
'message': 'something went wrong'
})
}
else{
resolve({
'status' : 'ok',
'message': 'here is your data'
})
}
})
axios("https://pokeapi.co/api/v2/pokemon")
.then(response => console.log("THEN: ", response))
.catch(error => console.log('ERROR: ', error))