From a6e0b7111dd2fcac70cf2e7eaf8a47fe36ddf690 Mon Sep 17 00:00:00 2001 From: PrashantSalaveJosh Date: Tue, 10 Oct 2023 17:13:40 +0530 Subject: [PATCH] completed javascript assignment 8 --- Q1.js | 20 ++++++++++++++++++++ Q2.js | 31 +++++++++++++++++++++++++++++++ Q3.js | 47 +++++++++++++++++++++++++++++++++++++++++++++++ Q4.js | 25 +++++++++++++++++++++++++ Q5.js | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ Q6.js | 34 ++++++++++++++++++++++++++++++++++ Q7.js | 30 ++++++++++++++++++++++++++++++ 7 files changed, 242 insertions(+) create mode 100644 Q1.js create mode 100644 Q2.js create mode 100644 Q3.js create mode 100644 Q4.js create mode 100644 Q5.js create mode 100644 Q6.js create mode 100644 Q7.js diff --git a/Q1.js b/Q1.js new file mode 100644 index 0000000..7b00730 --- /dev/null +++ b/Q1.js @@ -0,0 +1,20 @@ +/* +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 func = async (time) => { + console.log("Printing before"); + setTimeout(() => { + console.log(`Executed after ${time} seconds`); + }, time); + console.log("Printing after"); +}; + +func(5000); diff --git a/Q2.js b/Q2.js new file mode 100644 index 0000000..c310bd2 --- /dev/null +++ b/Q2.js @@ -0,0 +1,31 @@ +/* +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 +*/ + +//with await +async function getUsers(url) { + let response = await fetch(url); + return response.json(); +} + +let users = getUsers("https://reqres.in/api/users "); +users.then((result) => { + console.log(result); +}); + +//without await +function getUsersWithoutAwait(url) { + fetch(url) + .then((res) => res.json()) + .then((res) => console.log(res)) + .catch((err) => err); +} + +//this gives required data +getUsersWithoutAwait("https://reqres.in/api/users "); + +//this gives Promise { } +//Bcoz of JS Engine-> log function gets executed first, before even the promise resolves +// console.log({ users }); diff --git a/Q3.js b/Q3.js new file mode 100644 index 0000000..c617dc8 --- /dev/null +++ b/Q3.js @@ -0,0 +1,47 @@ +/* +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)); +*/ + +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)); + +//When the promise resolves, we get - "Response in then block: Test Resolve" +//As parameter passed in resolve function gets passed in then function of promise. + +//When the promise rejects, we get - +//Error caught in testAsyncFunction: Test Reject +// Response in then block: undefined + +//As we have written catch in the function itself the error gets executed there +//and in the next catch it returns undefined. So we get result as above \ No newline at end of file diff --git a/Q4.js b/Q4.js new file mode 100644 index 0000000..4f3db4f --- /dev/null +++ b/Q4.js @@ -0,0 +1,25 @@ +/* +4. 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)); +*/ + +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 + +//The static Promise.reject function returns a Promise that is rejected. +//It is a generic method diff --git a/Q5.js b/Q5.js new file mode 100644 index 0000000..ae98b8d --- /dev/null +++ b/Q5.js @@ -0,0 +1,55 @@ +/* +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)); + +*/ + +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); + }); + +//When promise resolves- +//Output:-Response in then block: Test Resolve + +//When promise rejects- +/*Output:-Error caught in testAsyncFunction Test Reject + Error in catch block: Error: Forced error + at /home/josh/javascript_training/Q5.js:32:11 +*/ + +/* +we have thrown a new error in the function of catch block of previous promise, +which gets catched in the next execution + */ diff --git a/Q6.js b/Q6.js new file mode 100644 index 0000000..cfae8ad --- /dev/null +++ b/Q6.js @@ -0,0 +1,34 @@ +/* +Q6. 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 getUsers = (url) => fetch(url); + +const newFunc = () => { + return new Promise((res, rej) => { + getUsers("https://reqres.in/api/users") + .then((users) => { + if (!users) { + rej("Not Found..."); + } + console.log("receivedData"); + setTimeout(() => { + res(users.json()); + }, 2000); + }) + .catch((err) => { + console.log(err); + }); + }); +}; + +newFunc() + .then((data) => { + console.log(data); + }) + .catch((err) => { + console.log(err); + }); diff --git a/Q7.js b/Q7.js new file mode 100644 index 0000000..ac44584 --- /dev/null +++ b/Q7.js @@ -0,0 +1,30 @@ +/* +Q7. 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. +(with async and await) +*/ + +//With Async and Await + +const getUsers = async (url) => await fetch(url); + +const func = (url) => { + return new Promise((res, rej) => { + getUsers(url) + .then((users) => { + if (!users) { + rej("Not Found..."); + } + console.log("receivedData"); + setTimeout(() => { + res(users.json()); + }, 2000); + }) + .catch((err) => { + console.log("Custom Error ", err); + }); + }); +}; + +func("https://reqres.in/api/users").then((users) => console.log(users));