From 5aaef6fe74b573482e4d8b48e39492aef9a975a5 Mon Sep 17 00:00:00 2001 From: Pushpak Raut Date: Thu, 31 Aug 2023 22:57:15 +0530 Subject: [PATCH 1/2] solution: solved the 5th challenge of promises async await (5) --- solutions/promises_async_await/5.js | 46 +++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 solutions/promises_async_await/5.js diff --git a/solutions/promises_async_await/5.js b/solutions/promises_async_await/5.js new file mode 100644 index 0000000..0251466 --- /dev/null +++ b/solutions/promises_async_await/5.js @@ -0,0 +1,46 @@ +const URL = "https://whatthecommit.com/index.json"; + + +//using .then and .catch +const fetchUsingThen = () => { + let requests = []; + for(let i = 0;i < 10; i++) { + requests.push(fetch(URL)) + } + + Promise.all(requests) + .then(responses => Promise.all(responses.map(result => result.json()))) + .then(data => console.log(data)) + .catch(err => console.log("Error: " + err)) +} + +fetchUsingThen(); + +//fetch using async await +const fetchAsyncAwait = async () =>{ + let requests = []; + for(let i = 0;i < 10; i++) { + requests.push(fetch(URL)) + } + + try{ + const responses = await Promise.all(requests); + const data = await Promise.all(responses.map(result => result.json())) + console.log(data) + } + catch(error){ + console.log("Error: " + error); + } +} + +fetchAsyncAwait() + + +// simple fetch for 1 time +const getFetch = () => { + fetch(URL) + .then(res => res.json()) + .then(data => console.log(data)); +} + +getFetch() \ No newline at end of file From fcd5a0c04da5ddafde6f4d7191bf8fc4fc390bfe Mon Sep 17 00:00:00 2001 From: Pushpak Raut Date: Fri, 1 Sep 2023 01:38:42 +0530 Subject: [PATCH 2/2] solution: updated the 5th challenge of promises async await (5) --- solutions/promises_async_await/5.js | 63 ++++++++++++++--------------- 1 file changed, 30 insertions(+), 33 deletions(-) diff --git a/solutions/promises_async_await/5.js b/solutions/promises_async_await/5.js index 0251466..5cabf80 100644 --- a/solutions/promises_async_await/5.js +++ b/solutions/promises_async_await/5.js @@ -1,46 +1,43 @@ const URL = "https://whatthecommit.com/index.json"; - -//using .then and .catch +// using .then and .catch const fetchUsingThen = () => { - let requests = []; - for(let i = 0;i < 10; i++) { - requests.push(fetch(URL)) - } + const requests = []; + for (let i = 0; i < 10; i++) { + requests.push(fetch(URL)); + } - Promise.all(requests) - .then(responses => Promise.all(responses.map(result => result.json()))) - .then(data => console.log(data)) - .catch(err => console.log("Error: " + err)) -} + Promise.all(requests) + .then((responses) => Promise.all(responses.map((result) => result.json()))) + .then((data) => console.log(data)) + .catch((err) => console.log("Error: " + err)); +}; fetchUsingThen(); -//fetch using async await -const fetchAsyncAwait = async () =>{ - let requests = []; - for(let i = 0;i < 10; i++) { - requests.push(fetch(URL)) - } - - try{ - const responses = await Promise.all(requests); - const data = await Promise.all(responses.map(result => result.json())) - console.log(data) - } - catch(error){ - console.log("Error: " + error); - } -} +// fetch using async await +const fetchAsyncAwait = async () => { + const requests = []; + for (let i = 0; i < 10; i++) { + requests.push(fetch(URL)); + } -fetchAsyncAwait() + try { + const responses = await Promise.all(requests); + const data = await Promise.all(responses.map((result) => result.json())); + console.log(data); + } catch (error) { + console.log("Error: " + error); + } +}; +fetchAsyncAwait(); // simple fetch for 1 time const getFetch = () => { - fetch(URL) - .then(res => res.json()) - .then(data => console.log(data)); -} + fetch(URL) + .then((res) => res.json()) + .then((data) => console.log(data)); +}; -getFetch() \ No newline at end of file +getFetch();