Skip to content
Snippets Groups Projects
Commit b70118fa authored by Andrej Rasevic's avatar Andrej Rasevic
Browse files

adding Exercise 4

parent ceda6e0c
No related branches found
No related tags found
No related merge requests found
...@@ -10,9 +10,9 @@ ...@@ -10,9 +10,9 @@
<script> <script>
async function makeMulipleRequests() { async function makeMulipleRequests() {
const p1 = await fetch('https://api.github.com/users/egonzal5'); const p1 = await fetch('https://api.github.com/users/arasevic');
console.log(p1); console.log(p1);
const p2 = await p1; const p2 = await p1.json();
console.log(p2); console.log(p2);
//}); //});
//const p2 = fetch('https://api.github.com/users/egonzal5').then(data => { //const p2 = fetch('https://api.github.com/users/egonzal5').then(data => {
......
...@@ -15,6 +15,7 @@ function getBookById(id) { ...@@ -15,6 +15,7 @@ function getBookById(id) {
// create a new promise // create a new promise
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
// using a settimeout to mimic a database // using a settimeout to mimic a database
console.log('creating promise inside getBookById')
setTimeout(() => { setTimeout(() => {
// find the post we want // find the post we want
const book = books.find(book => book.id === id); const book = books.find(book => book.id === id);
...@@ -31,11 +32,17 @@ function hydrateAuthor(book) { ...@@ -31,11 +32,17 @@ function hydrateAuthor(book) {
// create a new promise // create a new promise
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
// find the author // find the author
console.log('creating promise in hydrate Author')
const authorDetails = authors.find(author => author.name === book.author); const authorDetails = authors.find(author => author.name === book.author);
if(authorDetails) { if(authorDetails) {
// "hydrate" the post object with the author object // "hydrate" the post object with the author object
book.author = authorDetails; book.author = authorDetails;
resolve(book); setTimeout(() => {
console.log('this will print last')
}, 0)
setTimeout(() => {
resolve(book)
},0);
} else { } else {
reject(Error(`Can not find the author for book ${book}`)); reject(Error(`Can not find the author for book ${book}`));
} }
...@@ -53,15 +60,38 @@ getBookById(1) ...@@ -53,15 +60,38 @@ getBookById(1)
console.error(err); console.error(err);
}); });
for (let id of ids) { getBookById(2)
getBookById(id) .then(book => {
.then(book => { return hydrateAuthor(book);
return hydrateAuthor(book); })
}) .then(book => {
.then(book => { console.log(book);
console.log(book); })
}) .catch(err => {
.catch(err => { console.error(err);
console.error(err); });
});
} getBookById(3)
\ No newline at end of file .then(book => {
return hydrateAuthor(book);
})
.then(book => {
console.log(book);
})
.catch(err => {
console.error(err);
});
console.log('MIND BLOWN!!!!!!')
// for (let id of ids) {
// getBookById(id)
// .then(book => {
// return hydrateAuthor(book);
// })
// .then(book => {
// console.log(book);
// })
// .catch(err => {
// console.error(err);
// });
// }
...@@ -2,13 +2,15 @@ const promise = new Promise(function(resolve, reject) { ...@@ -2,13 +2,15 @@ const promise = new Promise(function(resolve, reject) {
console.log("Promise callback"); console.log("Promise callback");
resolve("foo"); resolve("foo");
}).then(function(result) { }).then(function(result) {
console.log("Promise callback (.then)"); console.log("Promise callback (.then)");//resolve(undefined)
}).then(function(result) { }).then(function(result) {
console.log(result) console.log(result)
console.log("Promise callback (.second then)"); console.log("Promise callback (.second then)");
return "bar" return "bar" //resolve
}).then((message) => console.log(message)); }).then((message) => {
console.log(message);
return "I LOVE PROMISES"
})
setTimeout(function() { setTimeout(function() {
console.log("event-loop cycle: Promise (fulfilled)", promise) console.log("event-loop cycle: Promise (fulfilled)", promise)
}, 0); }, 0);
......
## Exercise 4
Consider the following javascript code:
```javascript
setTimeout(() => console.log('This may or may not be last'), 4005)
console.log('this will run first')
new Promise(res =>{
console.log('inside first promise')
setTimeout(res,4000, 'Nikola')
console.log('at the end of first promise')
}).then(name => console.log(name))
let promise = new Promise((res) => {
console.log('inside second promise')
res(8)
})
promise.then(val => {
console.log('thennnnnnnning')
console.log(val)
})
console.log('this will not be last')
```
In the answer field below type the output from the following code (try and reason it out first and not run it) and then explain why the output is what it is using what we have covered in lecture about how javascript handles asynchronous code even though it has a single thread. Additionally, make sure to answer the following:
1. How many ticks of the javascript event loop does it take to execute the script to completion?
2. What is the maximum number of callbacks ever waiting on the macrotask queue during the script?
3. What is the maximum number of microtasks during each tick on the microtask queue?
### Answer:
(put your answer here)
# Exercise 4:
## Due Date: Thursday June 24, 11:59 PM
## Objectives: Explore the javascript event loop, job queue (microtask queue), task queue (macrotand async funtionality in a single thread environment.
## Specifications/Requirements
1. Description:
Look at the code snippet given in `exercise4.md` file. Below the code snippet provide what the output would be from executing that code and also provide a brief explanation using what we have learned about how javascript performs asynchronous tasks why the output is what it is.
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment