-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunctions.js
42 lines (38 loc) · 1.09 KB
/
functions.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
const path = require("path");
const readFile = (filePath = path.resolve("pokemons.json")) =>
new Promise((resolve, reject) => {
try {
const file = require(filePath);
resolve(file);
} catch (err) {
reject(err);
}
});
const getPokemons = () =>
new Promise(async (resolve, reject) => {
try {
let pokemons = await readFile();
pokemons = pokemons.map(each => {
return {
id: each.id,
name: each.name,
thumb: each.thumb,
types: each.types
};
});
resolve(pokemons);
} catch (err) {
reject(err);
}
});
const getPokemon = (id = 1) =>
new Promise(async (resolve, reject) => {
try {
const pokemons = await readFile();
const [pokemon] = pokemons.filter(each => each.id == id);
resolve(pokemon);
} catch (err) {
reject(err);
}
});
module.exports = { getPokemons, getPokemon };