-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathasynchronous.js
More file actions
92 lines (80 loc) · 2.07 KB
/
asynchronous.js
File metadata and controls
92 lines (80 loc) · 2.07 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
// 1st
// for (let i=1;i<=10;i++){
// console.log(i)
// }
// for(let j=11;j<=20;j++){
// setTimeout(()=>{
// console.log(j)
// },2000)
// }
//2nd
// function checkWeather(){
// return new Promise((res,rej)=>{
// let weather=Math.random()>0.5?'Sunny':'Rainy';
// //console.log(weather) to check if code was working or not
// if(weather=='Sunny'){
// res("Weather great for picnic!")
// }
// else if(weather=='Rainy'){
// rej("It might rain today!")
// }
// })
// }
// checkWeather()
// .then((data)=>{
// console.log(data)
// })
// .catch((err)=>{
// console.log(err)
// })
//3rd
// function makeDinner(){
// setTimeout(()=>{
// console.log("Boiling Water...")
// setTimeout(()=>{
// console.log("Adding Pasta...")
// setTimeout(()=>{
// console.log("Stirring Pasta...")
// setTimeout(()=>{
// console.log("Serving Meal...")
// },1000)
// },1000)
// },1000)
// },1000)
// }
// makeDinner()
//4th
// function fetchStockPrice(){
// return Math.floor(Math.random()*(500-100+1))+100;
// }
// function checkPrice(price){
// return new Promise((resolve, reject) => {
// if(price<400){
// resolve('Checking price again...')
// }
// else{
// reject('Alert: High price detected! Stopping checks.')
// }
// })
// }
// function startMonitoring(){
// let intervalId=setInterval(()=>{
// let price=fetchStockPrice();
// console.log(`Price: ${price}`);
// checkPrice(price)
// .then((message)=>{
// console.log(message)
// })
// .catch((err)=>{
// console.log(err)
// clearInterval(intervalId)
// })
// },2000)
// }
// startMonitoring()
//5th
let data=fetch('https://dummyjson.com/posts');
let response=data.then(res=>res.json());
response.then(data=>console.log(data))
.catch(()=>console.log('error fetching data'))
console.log(response)