diff --git a/Assignment_day02/question1.js b/Assignment_day02/question1.js new file mode 100644 index 0000000..f4457e4 --- /dev/null +++ b/Assignment_day02/question1.js @@ -0,0 +1,23 @@ +// Write a function that can stop execution of a function for the number of milliseconds +// sent as an argument +// Example: + +const sleep_for_a_while = async (func,t) =>{ + return new Promise((res,rej)=>{ + setTimeout(() => { + func(); + res(); + }, t); + }); +} + +const func = async () => { + console.log("Printing from func"); +} + +const foo = async () =>{ + console.log("Printing before"); + await sleep_for_a_while(func,5000); + console.log("Printing after"); +} +foo(); \ No newline at end of file diff --git a/Assignment_day02/question2.js b/Assignment_day02/question2.js new file mode 100644 index 0000000..5564e0f --- /dev/null +++ b/Assignment_day02/question2.js @@ -0,0 +1,14 @@ +// 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 + +const fetch_data = ()=>{ + fetch('https://reqres.in/api/users').then(response => { + return response.json(); + }).then(data => { + console.log(data.data); + }).catch((err)=>{ + console.log(err); + }); +} + +fetch_data() \ No newline at end of file diff --git a/Assignment_day02/question3.js b/Assignment_day02/question3.js new file mode 100644 index 0000000..c589d9f --- /dev/null +++ b/Assignment_day02/question3.js @@ -0,0 +1,24 @@ +// 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)); + +// Output: +// If Math.random() gives number greater than 0.5 then promise will be resolved and output would be +// Response in then block: Test Resolve + +// If Math.random() gives number less that equal to 0.5 then promise will be rejected and handled by catch bloack.so, output would be +// Error caught in testAsyncFunction: Test Reject +// Response in then block: undefined \ No newline at end of file diff --git a/Assignment_day02/question4.js b/Assignment_day02/question4.js new file mode 100644 index 0000000..4e049f4 --- /dev/null +++ b/Assignment_day02/question4.js @@ -0,0 +1,16 @@ +// 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)); + + // Output: + // Error in catch block Test static reject + + // Here testAsyncFunction() is rejecting the promise, so it will be handles by catch block \ No newline at end of file diff --git a/Assignment_day02/question5.js b/Assignment_day02/question5.js new file mode 100644 index 0000000..2daa9b2 --- /dev/null +++ b/Assignment_day02/question5.js @@ -0,0 +1,28 @@ +// 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)); + + //output: + // if Math.random() return value > 0.5 + // Response in then block: Test Resolve + + // if Math.random() return value <= 0.5 + // Error caught in testAsyncFunction Test Reject + // Error in catch block: Forced error \ No newline at end of file diff --git a/Assignment_day02/question6.js b/Assignment_day02/question6.js new file mode 100644 index 0000000..949f728 --- /dev/null +++ b/Assignment_day02/question6.js @@ -0,0 +1,20 @@ +// 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 promise_func = () => { + return new Promise((resolve, reject)=>{ + fetch('https://reqres.in/api/users').then((res)=>{ + setTimeout(()=>{ + resolve(res.json()); + },2000); + }).catch((err)=>{ + reject('error fetching data',err); + }); + }) +}; + +promise_func().then((data)=>{ + console.log(data.data); +}).catch((err)=>{ + console.log(err); +}) \ No newline at end of file diff --git a/Assignment_day02/question7.js b/Assignment_day02/question7.js new file mode 100644 index 0000000..3441878 --- /dev/null +++ b/Assignment_day02/question7.js @@ -0,0 +1,9 @@ +// Complete the above tasks with async/await. + +const fetch_data = async ()=>{ + const res = await fetch('https://reqres.in/api/users'); + const data = await res.json(); + console.log(data); +} + +fetch_data(); \ No newline at end of file