diff --git a/Assignment-8/Problem_1.js b/Assignment-8/Problem_1.js new file mode 100644 index 0000000..cf27457 --- /dev/null +++ b/Assignment-8/Problem_1.js @@ -0,0 +1,25 @@ +//1. Write a function that can stop execution of a function for the number of milliseconds sent as an argument +// Example: +// const func = async () => { +// console.log(“Printing before”) +// //Call your function here eg. sleep(3000) +// console.log(“Printing after”) +// () + +const sleep=(timeLimit)=>{ + return new Promise((resolve,reject)=>{ + setTimeout(()=>{ + resolve({data:{name:"naziya",lang:"javascript",eng:true}}) + },timeLimit) + }) +} + +console.log("printing before") + +const testAsyncFunction = async()=>{ + const data= await sleep(3000) + console.log(data) + console.log("Data is fetched") +} +testAsyncFunction() +console.log('Printing after') \ No newline at end of file diff --git a/Assignment-8/Problem_2.js b/Assignment-8/Problem_2.js new file mode 100644 index 0000000..52d9cb8 --- /dev/null +++ b/Assignment-8/Problem_2.js @@ -0,0 +1,22 @@ +//2. Using promises - write a function that fetches data from an API endpoint (GET https://reqres.in/api/users ). Log the data into the console once it is received + + +let promiseResult= fetch('https://reqres.in/api/users') +.then((res)=>{ + return res.json() +}) +.then((data)=>{ + console.log(data) +}) + + +// using async await + + +const fetchdata= async()=>{ + const data1= await fetch('https://reqres.in/api/users') + const sortedData= await data1.json() + console.log(sortedData) +} + +fetchdata() \ No newline at end of file diff --git a/Assignment-8/Problem_3.js b/Assignment-8/Problem_3.js new file mode 100644 index 0000000..829cf19 --- /dev/null +++ b/Assignment-8/Problem_3.js @@ -0,0 +1,24 @@ +//3. What will be printed to the console when the promise resolves and when it rejects? +const testAsyncFunction = () =>{ +return new Promise((resolve, reject) =>{ +if (Math.random() > 0.5) { +resolve('Test Resolve'); +} else { +reject('Test Reject'); +} +}).catch((err) =>{ +console.log('Error caught in testAsyncFunction: ', err); +}); +}; +testAsyncFunction() +.then((res) =>{ +console.log('Response in then block: ', res); +}) +.catch((err) => console.log('Error in catch block: ', err)) + +// In case where the Problem is resolved it will log +"Response in then block: Test Resolved" +//// In case where the Problem is rejected it will log +"Error caught in testAsyncFunction: Test Reject" +"Error in catch block: Test Reject" + diff --git a/Assignment-8/Problem_4.js b/Assignment-8/Problem_4.js new file mode 100644 index 0000000..7b5f141 --- /dev/null +++ b/Assignment-8/Problem_4.js @@ -0,0 +1,10 @@ +//What will be printed to the console? +const testAsyncFunction = () =>{ +return Promise.reject('Test static reject'); +}; +testAsyncFunction() +.then((res) =>{ +console.log('Response in then block', res); +}) +.catch((err) => console.log('Error in catch block', err)); + diff --git a/Assignment-8/Problem_5.js b/Assignment-8/Problem_5.js new file mode 100644 index 0000000..9442541 --- /dev/null +++ b/Assignment-8/Problem_5.js @@ -0,0 +1,24 @@ +//5. What will be printed to the console? +const testAsyncFunction = () =>{ + return new Promise((resolve, reject) =>{ + if (Math.random() > 0.5) { + resolve('Test Resolve'); + } else { + reject('Test Reject'); + } + }).catch((err) =>{ + console.log('Error caught in testAsyncFunction', err); + throw new Error('Forced error'); + }); + }; + testAsyncFunction() + .then((res) =>{ + console.log('Response in then block: ', res); + }) + .catch((err) => console.log('Error in catch block: ', err)); + +// In Case the promise gets rejected + +// 'Error caught in testAsyncFunction:Test Reject' +// Error:Forced error + diff --git a/Assignment-8/Problem_6.js b/Assignment-8/Problem_6.js new file mode 100644 index 0000000..6a784c6 --- /dev/null +++ b/Assignment-8/Problem_6.js @@ -0,0 +1,40 @@ +// //Create a promise that makes a fetch call, but resolves with the data only 2 seconds after the data has been received in the fetch. + +// const fetchdelay=()=>{ +// return new Promise((resolve,reject)=>{ +// setTimeout(()=>{ +// return resolve(fetch('https://reqres.in/api/users')) +// },5000) + +// }).then((res)=>{ +// return res.json() +// }).then((data)=>{ +// console.log(data) +// console.log('Data is fetched') +// }) + +// } +// fetchdelay() +// console.log('data fetching has started') + + +// with the help of async and await + +const fetchdelay=()=>{ + return new Promise((resolve,reject)=>{ + setTimeout(()=>{ + return resolve(fetch('https://reqres.in/api/users')) + },1000) + + }) + } + +let delayfunc=async()=>{ + let data= await fetchdelay() + let sorteddata =await data.json() + console.log(sorteddata) + console.log('Data is fetched') +} +delayfunc() +console.log('data fetching has started') +