333
schangxiang@126.com
2025-09-19 18966e02fb573c7e2bb0c6426ed792b38b910940
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# await-first
Wait the first event in a set of event pairs, then clean up after itself.
 
[![NPM version][npm-image]][npm-url]
[![build status][travis-image]][travis-url]
[![Test coverage][codecov-image]][codecov-url]
[![David deps][david-image]][david-url]
[![Known Vulnerabilities][snyk-image]][snyk-url]
[![npm download][download-image]][download-url]
 
[npm-image]: https://img.shields.io/npm/v/await-first.svg?style=flat-square
[npm-url]: https://npmjs.org/package/await-first
[travis-image]: https://img.shields.io/travis/node-modules/await-first.svg?style=flat-square
[travis-url]: https://travis-ci.org/node-modules/await-first
[codecov-image]: https://codecov.io/gh/node-modules/await-first/branch/master/graph/badge.svg
[codecov-url]: https://codecov.io/gh/node-modules/await-first
[david-image]: https://img.shields.io/david/node-modules/await-first.svg?style=flat-square
[david-url]: https://david-dm.org/node-modules/await-first
[snyk-image]: https://snyk.io/test/npm/await-first/badge.svg?style=flat-square
[snyk-url]: https://snyk.io/test/npm/await-first
[download-image]: https://img.shields.io/npm/dm/await-first.svg?style=flat-square
[download-url]: https://npmjs.org/package/await-first
 
## Install
 
```
$ npm install await-first --save
```
 
## Example
 
- `awaitFirst(ee, events)`
 
```js
const EventEmitter = require('events');
const awaitFirst = require('await-first');
 
async function waitMessageOrClose(ee) {
  const o = await awaitFirst(ee, [ 'message', 'close' ]);
 
  switch (o.event) {
    case 'message':
      const msg = o.args[0]; // [ 'hello world' ]
      console.log('new message =>', msg);
      break;
    case 'close':
      console.log('closed');
      break;
  }
}
 
const ee = new EventEmitter();
waitMessageOrClose(ee);
 
setTimeout(() => {
  ee.emit('message', 'hello world');
}, 1000);
```
 
- `obj.awaitFirst(events)`
 
```js
const net = require('net');
const awaitFirst = require('await-first');
 
async function connect() {
  const socket = net.connect(8080, '127.0.0.1');
  socket.awaitFirst = awaitFirst;
 
  try {
    // wait `connect` or `error` event
    await socket.awaitFirst([ 'connect', 'error' ]);
  } catch (err) {
    console.log(err);
  }
  // ...
}
 
connect();
```