Skip to content

Commit 3b7c7ac

Browse files
Using promises in fetch
1 parent 72a092d commit 3b7c7ac

File tree

1 file changed

+29
-8
lines changed

1 file changed

+29
-8
lines changed

promises/script.js

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,36 @@ function createPost(post){
3131
}
3232
// createPost({title:"Post three",body:"This is post three"}).then(getPosts).catch((err)=>console.log(err));
3333

34+
//Async/Await
35+
36+
// async function init(){
37+
// await createPost({title:"Post three",body:"This is post three"})
38+
// getPosts();
39+
// }
40+
41+
// init();
42+
43+
// Async / Await with fetch
44+
async function fetchPosts() {
45+
const res = await fetch(`https://jsonplaceholder.typicode.com/posts`);
46+
47+
const data = await res.json()
48+
console.log(data);
49+
}
50+
51+
fetchPosts();
52+
3453
//Promise.all
3554

36-
const promise1 = Promise.resolve("Hello world");
37-
const promise2 = 10;
38-
const promise3 = new Promise((res,rej)=>
39-
setTimeout(res,2000,"Goodbye")
40-
)
55+
// const promise1 = Promise.resolve("Hello world");
56+
// const promise2 = 10;
57+
// const promise3 = new Promise((res,rej)=>
58+
// setTimeout(res,2000,"Goodbye")
59+
// )
60+
61+
// const promise4 = fetch(`https://jsonplaceholder.typicode.com/posts`)
62+
63+
// const promise5 = fetch(`https://jsonplaceholder.typicode.com/posts`).then(res=>res.json())
64+
// Promise.all([promise1,promise2,promise3,promise4,promise5]).then(values=>console.log(values))
4165

42-
const promise4 = fetch(`https://jsonplaceholder.typicode.com/posts`)
4366

44-
const promise5 = fetch(`https://jsonplaceholder.typicode.com/posts`).then(res=>res.json())
45-
Promise.all([promise1,promise2,promise3,promise4,promise5]).then(values=>console.log(values))

0 commit comments

Comments
 (0)