Skip to content

Commit 623c380

Browse files
committed
completed course code
1 parent 67c8bf8 commit 623c380

File tree

6 files changed

+144
-1
lines changed

6 files changed

+144
-1
lines changed

04_apiExample.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ function fetchWithCallback(callback) {
3030

3131
const callback = function () {
3232
console.log('\nData fetch was completed with the https module!\n');
33-
}
33+
};
3434

3535
Number(process.argv[2]) === 1 ?
3636
fetchData() :

06_promises.js

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
function firstPromise (shouldFail = false, delay=1000, id=0) {
2+
console.log(`The promise with id:${id} is pending...`);
3+
return new Promise(function (resolve, reject) {
4+
setTimeout( function () {
5+
if (shouldFail) {
6+
return reject(new Error(`The promise with id:${id} has failed XD XD XD`));
7+
} else {
8+
return resolve(`The promise with id:${id} was fulfilled!`);
9+
}
10+
}, delay);
11+
});
12+
}
13+
14+
function main() {
15+
16+
17+
let shouldFail = Boolean(process.argv[2]);
18+
let shouldFailAll = Boolean(process.argv[3]);
19+
20+
21+
22+
firstPromise(shouldFail)
23+
.then (function (res) {
24+
console.log(res);
25+
})
26+
.catch(function (err) {
27+
console.log(err.message);
28+
});
29+
30+
31+
let promiseArr = [];
32+
for (let i=1; i < 6; i ++) {
33+
let shouldFail = false;
34+
if (i % 2 === 0) {
35+
shouldFail = shouldFailAll;
36+
}
37+
promiseArr.push(firstPromise(shouldFail, i * 1000, i));
38+
}
39+
40+
Promise.all(promiseArr)
41+
.then (function (res) {
42+
console.log('All promises were resolved:');
43+
console.log(res);
44+
})
45+
.catch (function (err) {
46+
console.log(err);
47+
});
48+
49+
50+
Promise.race(promiseArr)
51+
.then(function (res) {
52+
console.log('The following promise won the race *CLAP CLAP*');
53+
console.log(res);
54+
})
55+
.catch(function (err) {
56+
console.log('The following promise runined everything *CRY*');
57+
console.log(err);
58+
});
59+
60+
}
61+
62+
main();

07_chainedPromise.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
new Promise(function (resolve, reject) {
2+
3+
setTimeout(() => resolve(1), 1000);
4+
5+
}).then(function (result) {
6+
7+
console.log(result); // 1
8+
9+
return new Promise((resolve, reject) => { // (*)
10+
setTimeout(() => resolve(result * 2), 1000);
11+
});
12+
13+
}).then(function (result) { // (**)
14+
15+
console.log(result); // 2
16+
17+
return new Promise((resolve, reject) => {
18+
setTimeout(() => resolve(result * 2), 1000);
19+
});
20+
21+
}).then(function (result) {
22+
23+
console.log(result); // 4
24+
25+
});

08_chainedPromiseApi.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
const fetchData = require('./fetchData');
2+
3+
fetchData(true);
4+
5+

09_asyncAwait.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
const fetchData = require('./fetchData');
2+
3+
async function callFetch() {
4+
console.log('Calling fetch...');
5+
let comments = null;
6+
try {
7+
comments = await fetchData(false);
8+
} catch( err) {
9+
console.log(err);
10+
}
11+
12+
if (comments) {
13+
comments.forEach((comment, i) => {
14+
console.log(`\n${i}: ${comment.body}`);
15+
});
16+
}
17+
}
18+
19+
callFetch();

fetchData.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
const fetch = require('node-fetch');
2+
const urlUsers = 'https://jsonplaceholder.typicode.com/users';
3+
const urlPosts = 'https://jsonplaceholder.typicode.com/posts';
4+
const urlComments = 'https://jsonplaceholder.typicode.com/comments';
5+
6+
module.exports = function fetchData(isPrint = true) {
7+
return fetch(urlUsers)
8+
.then(res => res.json())
9+
.then(users => {
10+
let requiredUser = users[0].id;
11+
if (isPrint) console.log(`Querying user:\n\t${users[0].name}`);
12+
return fetch(`${urlPosts}?userId=${requiredUser}`)
13+
})
14+
.then(res => res.json())
15+
.then(posts => {
16+
let requiredPost = posts[0].id;
17+
if (isPrint) console.log(`\nQuerying post:\n\t${posts[0].title}`);
18+
return fetch(`${urlComments}?postId=${requiredPost}`)
19+
20+
})
21+
.then(res => res.json())
22+
.then(comments => {
23+
if (isPrint) console.log(`\nFinally found comments!!!`);
24+
comments.forEach((comment, i) => {
25+
if (isPrint) console.log(`\n${i}: ${comment.body}`);
26+
});
27+
// throw new Error('Error: no comments');
28+
return comments;
29+
// return Promise.resolve(comments);
30+
})
31+
.catch(err => console.log(err.message));
32+
};

0 commit comments

Comments
 (0)