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

Random String

Random String với độ dài tuỳ chọn

10 mẹo sử dụng array và object trong javascript

10 mẹo sử dụng array và object trong javascript

await axios get

await axios get

Await in a for loop

How to Await in a for loop

Ajax, call jQuery POST to node.js expressjs

Ajax, call jQuery POST to node.js expressjs