diff --git a/exercise001.js b/exercise001.js index 32c3694..650015e 100644 --- a/exercise001.js +++ b/exercise001.js @@ -1,14 +1,12 @@ -const promise = new Promise((resolve, reject) => { - // Generate random number between 0 and 9 - const randomInt = Math.floor(Math.random() * 10); - - if (randomInt % 2 === 0) { - // Success - setTimeout(() => resolve('done'), 2000); - } else { - // Failure - setTimeout(() => reject('error'), 2000); - } -}); - -// Your solution(s) to exercise001 here! +const getPromiseResponse = async (resolve, reject) => { + const randomInt = await Math.floor(Math.random() * 10); + if (randomInt % 2 === 0) { + console.log(resolve); + } else { + console.log(reject); + } +}; +getPromiseResponse( + `Yay! Promise resolved with response:done`, + `Boo. Promise rejected with response:error` +); diff --git a/exercise002.js b/exercise002.js index 9932585..a3b7de1 100644 --- a/exercise002.js +++ b/exercise002.js @@ -2,11 +2,14 @@ import fetch from "node-fetch"; const jsonTypicode = "https://jsonplaceholder.typicode.com/todos/1"; -const fetchData = (apiEndPoint) => { - fetch(apiEndPoint) - .then((response) => response.json()) - .then((json) => console.log(json)) - .catch((error) => console.log(error)); -}; +async function fetchData(apiEndPoint) { + try { + const response = await fetch(apiEndPoint); + const json = await response.json(); + console.log(json); + } catch { + console.log(error); + } +} fetchData(jsonTypicode); diff --git a/package.json b/package.json index 2889f42..1944cbb 100644 --- a/package.json +++ b/package.json @@ -8,7 +8,8 @@ "doc": "docs" }, "scripts": { - "test": "echo \"Error: no test specified\" && exit 1" + "test": "echo \"Error: no test specified\" && exit 1", + "start": "node docs/exercise001.js" }, "repository": { "type": "git",