-
Notifications
You must be signed in to change notification settings - Fork 57
Expand file tree
/
Copy pathAsyncAwait.js
More file actions
50 lines (40 loc) · 1.26 KB
/
AsyncAwait.js
File metadata and controls
50 lines (40 loc) · 1.26 KB
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
38
39
40
41
42
43
44
45
46
47
48
49
50
// we use both keyword simultaneoulsy
// means withour using async you cannot use await
// using async keyword we can make function as asynchronous function
// the functon which is using the async keywoord that will always return the promises
// we can avoid promises chaining with the async and await combination (somehow)
const fetchPost = async ()=> {
return 0;
}
// console.log( 'fetchPost' ,fetchPost())
async function fetchData2 (){
return 1
}
// console.log('fetchData2' ,fetchData2())
// fetchData2().then((res1)=>{
// console.log('fetchData2' , res1)
// })
// const resolvedData= async()=> {
// const data = await fetchData2();
// console.log("Await inside" , data)
// return data;
// }
// // resolvedData();
// console.log(resolvedData())
const resolvedMe = async () => {
const data = await new Promise((resolved , reject)=> {
setTimeout(()=>{
console.log("hey i am here")
resolved("Hye data is fine")
}, 10000)
})
console.log('data' , data)
}
resolvedMe();
const fetchPostData = async()=> {
console.log("started api call")
const res = await fetch('https://jsonplaceholder.typicode.com/posts');
const posts = await res.json();
console.log(posts ,"end api call")
}
fetchPostData();