-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Expand file tree
/
Copy pathindex.js
More file actions
90 lines (82 loc) · 2.12 KB
/
index.js
File metadata and controls
90 lines (82 loc) · 2.12 KB
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
89
90
import fs from 'fs';
import superagent from 'superagent';
// 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);
});
*/