1. 程式人生 > >promise-筆記

promise-筆記

col turn return height con err var bubuko mage

promise

技術分享圖片

技術分享圖片

技術分享圖片

技術分享圖片

封裝Promise

var fs = require(‘fs‘)

function pReadFile(filePath) {
  return new Promise(function (resolve, reject) {
    fs.readFile(filePath, ‘utf8‘, function (err, data) {
      if (err) {
        reject(err)
      } else {
        resolve(data)
      }
    })
  })
}

pReadFile(‘./data/a.txt‘)
  .then(
function (data) { console.log(data) return pReadFile(‘./data/b.txt‘) }) .then(function (data) { console.log(data) return pReadFile(‘./data/c.txt‘) }) .then(function (data) { console.log(data) })

promise-筆記