应用于浏览器和nodejs的异步等待效用 – awaiting

Awaiting

应用于浏览器和nodejs的异步/等待效用

  • Node.js >= 7.6.0
  • Edge >= 15
  • Firefox >= 52
  • Chrome >= 55
  • Safari >= 10.1
  • Opera >= 44
  • iOS Safari >= 10.3


安装

In node

yarn 或者 npm:

$ yarn add awaiting
$ npm install --save awaiting

然后:

const a = require('awaiting')

main()

async function main() {
  await a.delay(1000)
}

在浏览器中

使用 browser/awaiting.js :

<script src='awaiting.js'></script>
<script>
  const a = awaiting

  main()
  
  async function main() {
    await a.delay(1000)
  }
</script>

示例

// wait for a second
await a.delay(1000)

// limit a fetch to five seconds
const image = await a.limit(fetch('flowers.jpg'), 5000)

// await events
await a.event(server, 'listen')

// await callbacks
const contents = await a.callback(fs.readFile, 'foo.txt')

// map an array to an async function with 3 instances running in parallel
const pages = await a.map(urls, 3, fetch)

// await successes (ignoring errors)
const file = await a.success(getNetworkFile())

Building

$ yarn build

Testing

$ yarn install
$ yarn test
$ yarn test:browser
$ yarn coverage:report

本站文章除注明转载外,均为本站原创或编译
转载请明显位置注明出处:应用于浏览器和nodejs的异步等待效用 – awaiting