khoa-pham-async-await

Promise.all


1. Nội dung

2. Ví dụ

//todo: Promise basic
var fs = require('fs');

let readFilePromise = (path) => {
  return new Promise((resolve, reject) => {
    fs.readFile(path, { encoding: 'utf-8' }, (err, data) => {
      if (err) return reject(err);
      return resolve(data);
    });
  });
};

//! Đọc lần lượt từng file
readFilePromise('../docs/test1.txt')
  .then((test1) => {
    console.log(test1);
    return readFilePromise('../docs/test2.txt');
  })
  .then((test2) => {
    console.log(test2);
    return readFilePromise('../docs/test3.txt');
  })
  .then((test3) => console.log(test3))
  .catch((err) => console.log(err + ''));
let onDone = (data) => console.log(data);
let onError = (error) => console.log(error + '');

// Viết ngắn gọn lại
var fs = require('fs');

let readFilePromise = (path) => {
  return new Promise((resolve, reject) => {
    fs.readFile(path, { encoding: 'utf-8' }, (err, data) => {
      if (err) return reject(err);
      return resolve(data);
    });
  });
};

readFilePromise('../docs/test1.txt')
  .then(onDone)
  .then(() => readFilePromise('../docs/test2.txt'))
  .then(onDone)
  .then(() => readFilePromise('../docs/test3.txt'))
  .then(onDone)
  .catch(onError);

3. Promise all

var fs = require('fs');
let readFilePromise = (path) => {
  return new Promise((resolve, reject) => {
    fs.readFile(path, { encoding: 'utf-8' }, (err, data) => {
      if (err) return reject(err);
      return resolve(data);
    });
  });
};

Promise.all([
  readFilePromise('../docs/test1.txt'),
  readFilePromise('../docs/test2.txt'),
  readFilePromise('../docs/test3.txt'),
])
  .then((arr) => console.log(arr))
  .catch((err) => console.log(err + ''));

Promise all

Promise all Error

4. Bài tập

/**
 * Sử dụng Promise.all + axios để tải về 3 đường link sau cùng lúc và hiển thị ra kết quả:
 * https://jsonplaceholder.typicode.com/todos/1
 * https://jsonplaceholder.typicode.com/todos/2
 * https://jsonplaceholder.typicode.com/todos/3
 */
const axios = require('axios');

let axiosPromise = (path) => {
  return new Promise((resolve, reject) => {
    axios
      .get(path)
      .then((response) => resolve(response.data))
      .catch((error) => reject('Lỗi link: ' + error.config.url));
  });
};

let newArr = Promise.all([
  axiosPromise('https://jsonplaceholder.typicode.com/todos/1'),
  axiosPromise('https://jsonplaceholder.typicode.com/todos/2'),
  axiosPromise('https://jsonplaceholder.typicode.com/todos/3'),
])
  .then((arr) => arr)
  .catch((error) => error);

console.log(newArr); // <pending>

setTimeout(() => {
  console.log(newArr);
}, 2000);

Promise all Axios

let newArr = Promise.all([
  axiosPromise('https://jsonplaceholder.typicode.com/todos/1'),
  axiosPromise('https://jsonplaceholder.typicode.com/todos/222222'),
  axiosPromise('https://jsonplaceholder.typicode.com/todos/3'),
])
  .then((arr) => arr)
  .catch((error) => error);

console.log(newArr); // <pending>

setTimeout(() => {
  console.log(newArr);
}, 2000);

Promise all Axios Error