Files
  • index.js
  • package-lock.json
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
const request = require('request');

// Khởi tạo chung 1 phương thức có tham số là endpoint
const getDataFromApi = endpoint => (
  new Promise(function(resolve, reject) {
    request(endpoint, function (error, response, body) {
      let info = {};

      if (error) {
        return reject(error);
      }

      if (response.statusCode == 200) {
        info = JSON.parse(body);
        return resolve(info);
      }
    });

  })

);

let users = getDataFromApi('http://5c43e48357499f00143cedd1.mockapi.io/api/users');
let categories = getDataFromApi('http://5c43e48357499f00143cedd1.mockapi.io/api/categories');
let products = getDataFromApi('http://5c43e48357499f00143cedd1.mockapi.io/api/products');

Promise.all([users, categories, products])
  .then(function([users, categories, products]) {
    console.log({ users, categories, products });
  });
node v10.15.2 linux/amd64