promise in javascript

promise,tipjs

const fs = require('fs')

const readFileWithPromise = file => {
  return new Promise((resolve, reject) => {
    fs.readFile(file, (err, data) => {
      if (err) {
        reject(err)
      } else {
        resolve(data)
      }
    })
  })
}

readFileWithPromise('/etc/passwd')
  .then(data => {
    console.log(data.toString())
    return readFileWithPromise('/etc/profile')
  })
  .then(data => {
    console.log(data.toString())
  })
  .catch(err => {
    console.log(err)
  })

Có thể bạn đã miss một số snippets code

Convert Array-like to True Array

Convert Array-like to True Array

Sự khác biệt giữa substr và substring

Sự khác biệt giữa substr() và substring() chức năng trong JS là gì?

Sử dụng Promise.all với fetch

Sử dụng Promise.all hiệu quả sẽ làm cho hiệu suất của code chúng ta tăng lên đáng kể. Và mã sau đây giúp các bạn có thể get được nhiều data mà không ảnh hưởng tới performance.

flatMap()

Use flatMap() creates a new array with sub-array elements flattened by specified depth.

JavaScript: async/await with forEach

JavaScript: async/await with forEach()