-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
88 lines (80 loc) · 2.18 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
const fs = require('fs');
const superagent = require('superagent');
const readFilePro = (filePath) => {
return new Promise((resolve, reject) => {
fs.readFile(filePath, 'utf-8', (err, data) => {
if (err) reject('I could not find the file 🥲');
resolve(data);
});
});
};
const writeFilePro = (filePath, data) => {
return new Promise((resolve, reject) => {
fs.writeFile(filePath, data, (err) => {
if (err) reject('Could not write the file 🥲');
resolve('Sucess');
});
});
};
const getDogPic = async () => {
try {
const data = await readFilePro(`${__dirname}/dog.txt`);
console.log(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 res4Pro = superagent.get(
`https://dog.ceo/api/breed/${data}/images/random`
);
const res5Pro = superagent.get(
`https://dog.ceo/api/breed/${data}/images/random`
);
const all = await Promise.all([
res1Pro,
res2Pro,
res3Pro,
res4Pro,
res5Pro,
]);
const imgs = all.map((el) => el.body.message);
console.log(imgs);
writeFilePro('dog-img.txt', imgs.join('\n'));
console.log('Random dog saved to file');
} catch (err) {
console.log(err);
throw err;
}
return '2: Ready 🐶';
};
getDogPic();
// (async () => {
// try {
// console.log('1: I will get Dog pics');
// console.log(await getDogPic());
// console.log('3: Done getting dog pics');
// } catch (err) {
// console.log('ERROR 💥');
// }
// })();
// getDogPic();
// readFilePro(`${__dirname}/dog.txt`)
// .then((data) => {
// console.log(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 saved to file');
// })
// .catch((err) => {
// console.log(err.message);
// });