From 2f2f398748f472fd63c6c7ce76f75743791abb16 Mon Sep 17 00:00:00 2001 From: LecomteAdrien Date: Mon, 15 Feb 2021 17:57:24 +0100 Subject: [PATCH] :sparkles: pretier --- .babelrc.json | 7 ++--- index.js | 86 +++++++++++++++++---------------------------------- 2 files changed, 30 insertions(+), 63 deletions(-) diff --git a/.babelrc.json b/.babelrc.json index b338fa8..8cf4a3a 100644 --- a/.babelrc.json +++ b/.babelrc.json @@ -1,11 +1,8 @@ { - "presets": [ - "@babel/preset-stage-3", - "@babel/preset-env" - ], + "presets": ["@babel/preset-stage-3", "@babel/preset-env"], "plugins": [ "@babel/plugin-proposal-optional-chaining", "@babel/plugin-transform-destructuring", "@babel/plugin-proposal-nullish-coalescing-operator" ] -} \ No newline at end of file +} diff --git a/index.js b/index.js index 402b027..82e7346 100644 --- a/index.js +++ b/index.js @@ -5,51 +5,56 @@ import axios from 'axios'; const app = fastify({ logger: true }); const catFactsurl = `https://cat-fact.herokuapp.com/facts/random?animal_type=cat&amount=3`; - const loadCatFact = async () => { - try { - const {data} = await axios - .get(catFactsurl) + const { data } = await axios.get(catFactsurl); console.log(data); - return data.map(item => item.text); - }catch (err){ - console.log("\n" + err); + return data.map((item) => item.text); + } catch (err) { + console.log('\n' + err); return null; } }; -const loadDate = async (coutrycode) =>{ - try{ - const {data} = await axios - .get(`https://date.nager.at/api/v2/PublicHolidays/2021/${coutrycode}`) - console.log(data) +const loadDate = async (coutrycode) => { + try { + const { data } = await axios.get( + `https://date.nager.at/api/v2/PublicHolidays/2021/${coutrycode}`, + ); + console.log(data); return data; - }catch (err){ + } catch (err) { console.log(err); return null; } -} +}; -const loadImage = async () =>{ - try{ - const {data:{image}} = await axios - .get(`https://randomfox.ca/floof/`) +const loadImage = async () => { + try { + const { + data: { image }, + } = await axios.get(`https://randomfox.ca/floof/`); console.log(image); - return image - }catch (err){ + return image; + } catch (err) { console.log(err); return null; } -} +}; app.post('/', async (req, res) => { let coutrycode = req.body?.countryCode; console.log(coutrycode); - const [foxPicture, catFacts, holidays, ]= await Promise.all([loadImage(), loadCatFact(), loadDate(coutrycode),]); + const [foxPicture, catFacts, holidays] = await Promise.all([ + loadImage(), + loadCatFact(), + loadDate(coutrycode), + ]); return { - foxPicture, catFacts, holidays, + foxPicture, + catFacts, + holidays, }; }); @@ -63,38 +68,3 @@ const start = async () => { } }; start(); - -/* -const getPeople = async (id, extraDelay) => { - const response = await fetch( - `https://jsonplaceholder.typicode.com/todos/${id}` - ); - const json = await response.json(); - if (extraDelay) { - await timeout(extraDelay); - } - return json; -}; - -const getFirst5 = async (isAll) => { - console.log("START"); - if (isAll) { - const persons = await Promise.all([ - getPeople(1, 1000), - getPeople(2, 1000), - getPeople(3) - ]); - console.log(persons); - } else { - const first = await getPeople(1, 1000); - const sec = await getPeople(2, 1000); - const thrid = await getPeople(3); - console.log([first, sec, thrid]); - } - return; -}; - - - -getFirst5(false); -*/