Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Evelyn #23

Open
wants to merge 36 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
65b7a9a
learnings on July 23 2021
Jul 24, 2021
0e96e2f
learnings on July 26th, 2021
Jul 27, 2021
fe7b323
the dom
Jul 28, 2021
e4f10c1
chapter 6 finished learning
Jul 30, 2021
9a6d1e1
chapter 7 form & form events
Jul 30, 2021
69594b3
created a Ninja quiz
Jul 30, 2021
bf95c7a
practiced once
Jul 30, 2021
6cb9391
practice twice
Jul 30, 2021
2ce66ef
switching computer
Evelyn-ZYW Aug 30, 2021
3fb03a1
chapter 9 learnings
Evelyn-ZYW Aug 30, 2021
a217b0b
learnings of the mini project - todos
Evelyn-ZYW Aug 31, 2021
5132337
understanding Async in a very basic level
Evelyn-ZYW Sep 3, 2021
fe7c3eb
understanding HTTP request & JSON
Evelyn-ZYW Sep 3, 2021
d7aef64
understanding promises and how to use it in a request
Evelyn-ZYW Sep 3, 2021
5387bb0
chain promises together
Evelyn-ZYW Sep 3, 2021
933384b
understanding fetch() and async & await
Evelyn-ZYW Sep 4, 2021
e0acc1d
understanding throwing & catching errors
Evelyn-ZYW Sep 4, 2021
e0ee6fd
object shorthand notation
Evelyn-ZYW Sep 16, 2021
e5095a2
get reference of the card and details elements
Evelyn-ZYW Sep 16, 2021
1006f26
created updateUI function to receive the data from updateCity functio…
Evelyn-ZYW Sep 16, 2021
d6eedbc
destructuring - an easy way to get properties from an object and stor…
Evelyn-ZYW Sep 16, 2021
b9788f2
get references of the time and icon elements; added icons and imgs
Evelyn-ZYW Sep 16, 2021
7fe227c
use Ternary Operator instead of regular if elase statement
Evelyn-ZYW Sep 16, 2021
909dbfe
localStorage: 1. setItem 2. getItem 3. removeItem 4. clear
Evelyn-ZYW Sep 19, 2021
819ef71
localStorage for the weather app
Evelyn-ZYW Sep 19, 2021
2917f35
class inheritance
Evelyn-ZYW Sep 21, 2021
9f20336
constructor inheritance & super()
Evelyn-ZYW Sep 21, 2021
4fa64fb
prototype model
Evelyn-ZYW Sep 21, 2021
73bae52
prototypal inheritance
Evelyn-ZYW Sep 21, 2021
aa543a1
making a Forecast class for the weather app
Evelyn-ZYW Sep 21, 2021
c2c7415
firebase
Evelyn-ZYW Sep 22, 2021
cf00bfc
deleted node_modules folder
Evelyn-ZYW Sep 22, 2021
72355b6
real-time listeners enable real-time update to the UI when adding or …
Evelyn-ZYW Sep 22, 2021
317f8e4
unsubscribe from the collection changes
Evelyn-ZYW Sep 22, 2021
1430167
cleared some unused files and codes
Evelyn-ZYW Sep 22, 2021
cada1e3
real-time chatroom done
Evelyn-ZYW Oct 4, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
understanding promises and how to use it in a request
  • Loading branch information
Evelyn-ZYW committed Sep 3, 2021
commit d7aef64474de7b7771d450c7c4cbf33396497075
7 changes: 5 additions & 2 deletions chapter_12_Async/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@
<!-- <script src="sandbox1.js"></script> -->
<!-- <script src="http-request-1.js"></script> -->
<!-- <script src="http-request-2.js"></script> -->
<script src="jason-data.js"></script>
<!-- <script src="datefns.js"></script> -->
<!-- <script src="jason-data-1.js"></script> -->
<!-- <script src="jason-data-2.js"></script> -->
<!-- <script src="promise-1.js"></script> -->
<script src="promise-2.js"></script>
<!-- <script src="promise-3.js"></script> -->
</body>
</html>
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,9 @@ const getTodos = (callback) => {
}
})

// request.open('GET', 'https://jsonplaceholder.typicode.com/todos/')
request.open('GET', 'todos.json')
request.open('GET', 'https://jsonplaceholder.typicode.com/todos/')

// request.open('GET', 'todos.json')

request.send();
}
Expand Down
27 changes: 27 additions & 0 deletions chapter_12_Async/jason-data-2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
//if we have several files(endpoints) that we need to get one after the other
const getTodos = (apis, callback) => {
const request = new XMLHttpRequest();

request.addEventListener('readystatechange', ()=>{
if(request.readyState === 4 && request.status === 200){
const data = JSON.parse(request.responseText)
callback(undefined, data);
} else if(request.readyState === 4){
callback('could not fetch data', undefined);
}
})

request.open('GET', apis)
request.send();
}

//this is callback hell situation, it's very messy when there are many requests/callbacks - and this is when PROMISES comes into play
getTodos('todos/luigi.json', (err, data)=>{
console.log(data)
getTodos('todos/mario.json', (err, data)=>{
console.log(data)
getTodos('todos/shaun.json', (err, data)=>{
console.log(data)
})
})
});
45 changes: 45 additions & 0 deletions chapter_12_Async/promise-1.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
//if we have several files(endpoints) that we need to get one after the other
const getTodos = (apis, callback) => {
const request = new XMLHttpRequest();

request.addEventListener('readystatechange', ()=>{
if(request.readyState === 4 && request.status === 200){
const data = JSON.parse(request.responseText)
callback(undefined, data);
} else if(request.readyState === 4){
callback('could not fetch data', undefined);
}
})

request.open('GET', apis)
request.send();
}

//promise example

const getSomething = () => {
// step 1: when we use promises the first thing we do is to return a new promise
return new Promise((resolve, reject)=>{
//fetch something
// resolve('some data');
reject('some error');
});
}

//when we resolve something in a promise, it fires the callback(first parametor) inside the then method, and that callback function takes the data we pass through to the resolve function.
//same thing with the reject method, when promise has a reject outcome, the callback(second parameter) will fire inside the then method, and that callback function takes the data we pass through to the reject function.
// getSomething().then((data)=>{
// console.log(data); //will display 'some data'
// }, (err)=>{
// console.log(err) //will display 'some err'
// })


//Below works the same but looks neater, especially when it comes to chainning promises together.
//If the promise gets resolved, then it fires the callback in the then() method. If it get rejected, instead it comes to the catch() method and fires the callback inside of it.

getSomething().then(data => {
console.log(data)
}).catch(err => {
console.log(err)
})
24 changes: 24 additions & 0 deletions chapter_12_Async/promise-2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
//if we have several files(endpoints) that we need to get one after the other
const getTodos = (apis) => {
return new Promise((resolve, reject)=>{
const request = new XMLHttpRequest();

request.addEventListener('readystatechange', ()=>{
if(request.readyState === 4 && request.status === 200){
const data = JSON.parse(request.responseText)
resolve(data)
} else if(request.readyState === 4){
reject('encountered some error!')
}
})
request.open('GET', apis)
request.send();
})

}

getTodos('todos/luigi.json').then(data=>{
console.log(data)
}).catch(err => {
console.log(err)
})
Empty file added chapter_12_Async/promise-3.js
Empty file.
5 changes: 5 additions & 0 deletions chapter_12_Async/todos/luigi.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[
{"text": "do the plumming", "author": "Luigi"},
{"text": "avoid mario", "author": "Luigi"},
{"text": "go kart racing", "author": "Luigi"}
]
5 changes: 5 additions & 0 deletions chapter_12_Async/todos/mario.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[
{"text": "make fun of luigi", "author": "Mario"},
{"text": "rescue peach (again)", "author": "Mario"},
{"text": "go kart racing", "author": "Mario"}
]
5 changes: 5 additions & 0 deletions chapter_12_Async/todos/shaun.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[
{"text": "play mariokart", "author": "Shaun"},
{"text": "buy some bread", "author": "Shaun"},
{"text": "take a nap", "author": "Shaun"}
]