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

Detect when your site is visible to users

Detect when your site is visible to users

Lấy tháng hiện tại trong javascript

Lấy tháng hiện tại trong javascript

Converting Object to an Array

Converting Object to an 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ì?

Await in a for loop

How to Await in a for loop