mirror of
https://github.com/kevin-DL/complete-node-bootcamp.git
synced 2026-01-12 03:15:12 +00:00
Initial commit 🚀
This commit is contained in:
88
3-asynchronous-JS/final/index.js
Normal file
88
3-asynchronous-JS/final/index.js
Normal file
@@ -0,0 +1,88 @@
|
||||
const fs = require('fs');
|
||||
const superagent = require('superagent');
|
||||
|
||||
const readFilePro = file => {
|
||||
return new Promise((resolve, reject) => {
|
||||
fs.readFile(file, (err, data) => {
|
||||
if (err) reject('I could not find that file 😢');
|
||||
resolve(data);
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
const writeFilePro = (file, data) => {
|
||||
return new Promise((resolve, reject) => {
|
||||
fs.writeFile(file, data, err => {
|
||||
if (err) reject('Could not write file 😢');
|
||||
resolve('success');
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
const getDogPic = async () => {
|
||||
try {
|
||||
const data = await readFilePro(`${__dirname}/dog.txt`);
|
||||
console.log(`Breed: ${data}`);
|
||||
|
||||
const res1Pro = superagent.get(
|
||||
`https://dog.ceo/api/breed/${data}/images/random`
|
||||
);
|
||||
const res2Pro = superagent.get(
|
||||
`https://dog.ceo/api/breed/${data}/images/random`
|
||||
);
|
||||
const res3Pro = superagent.get(
|
||||
`https://dog.ceo/api/breed/${data}/images/random`
|
||||
);
|
||||
const all = await Promise.all([res1Pro, res2Pro, res3Pro]);
|
||||
const imgs = all.map(el => el.body.message);
|
||||
console.log(imgs);
|
||||
|
||||
await writeFilePro('dog-img.txt', imgs.join('\n'));
|
||||
console.log('Random dog image saved to file!');
|
||||
} catch (err) {
|
||||
console.log(err);
|
||||
|
||||
throw err;
|
||||
}
|
||||
return '2: READY 🐶';
|
||||
};
|
||||
|
||||
(async () => {
|
||||
try {
|
||||
console.log('1: Will get dog pics!');
|
||||
const x = await getDogPic();
|
||||
console.log(x);
|
||||
console.log('3: Done getting dog pics!');
|
||||
} catch (err) {
|
||||
console.log('ERROR 💥');
|
||||
}
|
||||
})();
|
||||
|
||||
/*
|
||||
console.log('1: Will get dog pics!');
|
||||
getDogPic()
|
||||
.then(x => {
|
||||
console.log(x);
|
||||
console.log('3: Done getting dog pics!');
|
||||
})
|
||||
.catch(err => {
|
||||
console.log('ERROR 💥');
|
||||
});
|
||||
*/
|
||||
/*
|
||||
readFilePro(`${__dirname}/dog.txt`)
|
||||
.then(data => {
|
||||
console.log(`Breed: ${data}`);
|
||||
return superagent.get(`https://dog.ceo/api/breed/${data}/images/random`);
|
||||
})
|
||||
.then(res => {
|
||||
console.log(res.body.message);
|
||||
return writeFilePro('dog-img.txt', res.body.message);
|
||||
})
|
||||
.then(() => {
|
||||
console.log('Random dog image saved to file!');
|
||||
})
|
||||
.catch(err => {
|
||||
console.log(err);
|
||||
});
|
||||
*/
|
||||
Reference in New Issue
Block a user