-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontenedor.js
36 lines (30 loc) · 954 Bytes
/
contenedor.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
const { promises: fs} = require('fs')
class Contenedor {
constructor(archivo){
this.archivo = archivo;
}
async getRandom(){
try {
const productos = await this.getAll()
//const random = Math.random() * productos.length | 0 ;
//const rProduct = productos[random]
let random = Math.floor(Math.random() * productos.length);
const rProduct = await productos[random]
console.log(rProduct)
return rProduct;
} catch (error) {
return error
}
}
async getAll(){
try {
const objs = await fs.readFile(this.archivo, 'utf-8');
console.log(objs);
return JSON.parse(objs);
} catch (error) {
return error;
}
}
};
const archivo1 = new Contenedor("./productos.txt")
module.exports = archivo1