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

Await in a for loop

How to Await in a for loop

Extend Object javascript

Extend Object javascript

Merge Array

How to combine two sorted arrays into one? We can do this easily with the spread operator.

serialize Form

Sử dụng formdata constructor để convert form, FormData, array.from(). Kết hợp window.encodeURIComponent() để encode từng value trong form.

How to reverse an Array?

Reversing an array in JS is really simple with the standard array method. Like below