IT大道IT大道

首页 >  技术 > async/await to rescue: Promisify all Node.js builtin async methods

async/await to rescue: Promisify all Node.js builtin async methods

原文 https://github.com/pd4d10/promisify-all 2017-10-12 06:01:11 0 评论

promisify-all

Promisify all Node.js builtin async methods using util.promisify

Installation

npm install --save promisify-all

Usage

Add the following code to the start of your Node.js project:

const promisifyAll = require('promisify-all')

promisifyAll()

Then you have promisified methods such as fs.readFileAsync and fs.writeFileAsync :

const fs = require('fs')

try {
  const content = await fs.readFileAsync('/path/to/file', 'utf8')
  console.log('Read file succeed! Content: ', content)

  await fs.writeFileAsync('/path/to/another/file', content)
  console.log('Write file succeed!')
} catch (err) {
  // Handle errors here
}

Options

suffix

Call promisifyAll as follows to customize suffix. The default suffix is Async .

promisifyAll({
  suffix: 'YourSuffix'
})

Promisified methods list

See here

License

MIT

Tags:Node.js

猜你喜欢

标签列表