├── .autod.conf.js ├── .eslintignore ├── .eslintrc ├── .gitignore ├── .travis.yml ├── History.md ├── LICENSE ├── README.md ├── appveyor.yml ├── lib └── index.js ├── package.json └── test └── index.test.js /.autod.conf.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | module.exports = { 4 | write: true, 5 | prefix: '^', 6 | devprefix: '^', 7 | exclude: [], 8 | devdep: [ 9 | 'mm', 10 | 'autod', 11 | 'egg-ci', 12 | 'egg-bin', 13 | 'eslint', 14 | 'eslint-config-egg', 15 | ], 16 | }; 17 | -------------------------------------------------------------------------------- /.eslintignore: -------------------------------------------------------------------------------- 1 | test/fixtures 2 | test/benchmark 3 | benchmark 4 | coverage -------------------------------------------------------------------------------- /.eslintrc: -------------------------------------------------------------------------------- 1 | { 2 | "extends": "eslint-config-egg" 3 | } 4 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Logs 2 | logs 3 | *.log 4 | npm-debug.log* 5 | yarn-debug.log* 6 | yarn-error.log* 7 | 8 | # Runtime data 9 | pids 10 | *.pid 11 | *.seed 12 | *.pid.lock 13 | 14 | # Directory for instrumented libs generated by jscoverage/JSCover 15 | lib-cov 16 | 17 | # Coverage directory used by tools like istanbul 18 | coverage 19 | 20 | # nyc test coverage 21 | .nyc_output 22 | 23 | # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) 24 | .grunt 25 | 26 | # Bower dependency directory (https://bower.io/) 27 | bower_components 28 | 29 | # node-waf configuration 30 | .lock-wscript 31 | 32 | # Compiled binary addons (http://nodejs.org/api/addons.html) 33 | build/Release 34 | 35 | # Dependency directories 36 | node_modules/ 37 | jspm_packages/ 38 | 39 | # Typescript v1 declaration files 40 | typings/ 41 | 42 | # Optional npm cache directory 43 | .npm 44 | 45 | # Optional eslint cache 46 | .eslintcache 47 | 48 | # Optional REPL history 49 | .node_repl_history 50 | 51 | # Output of 'npm pack' 52 | *.tgz 53 | 54 | # Yarn Integrity file 55 | .yarn-integrity 56 | 57 | # dotenv environment variables file 58 | .env 59 | 60 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | sudo: false 2 | language: node_js 3 | node_js: 4 | - '6' 5 | - '8' 6 | - '9' 7 | install: 8 | - npm i npminstall && npminstall 9 | script: 10 | - npm run ci 11 | after_script: 12 | - npminstall codecov && codecov 13 | -------------------------------------------------------------------------------- /History.md: -------------------------------------------------------------------------------- 1 | 2 | 1.0.0 / 2017-11-24 3 | ================== 4 | 5 | * feat: implement await first module 6 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 zōng yǔ 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # await-first 2 | Wait the first event in a set of event pairs, then clean up after itself. 3 | 4 | [![NPM version][npm-image]][npm-url] 5 | [![build status][travis-image]][travis-url] 6 | [![Test coverage][codecov-image]][codecov-url] 7 | [![David deps][david-image]][david-url] 8 | [![Known Vulnerabilities][snyk-image]][snyk-url] 9 | [![npm download][download-image]][download-url] 10 | 11 | [npm-image]: https://img.shields.io/npm/v/await-first.svg?style=flat-square 12 | [npm-url]: https://npmjs.org/package/await-first 13 | [travis-image]: https://img.shields.io/travis/node-modules/await-first.svg?style=flat-square 14 | [travis-url]: https://travis-ci.org/node-modules/await-first 15 | [codecov-image]: https://codecov.io/gh/node-modules/await-first/branch/master/graph/badge.svg 16 | [codecov-url]: https://codecov.io/gh/node-modules/await-first 17 | [david-image]: https://img.shields.io/david/node-modules/await-first.svg?style=flat-square 18 | [david-url]: https://david-dm.org/node-modules/await-first 19 | [snyk-image]: https://snyk.io/test/npm/await-first/badge.svg?style=flat-square 20 | [snyk-url]: https://snyk.io/test/npm/await-first 21 | [download-image]: https://img.shields.io/npm/dm/await-first.svg?style=flat-square 22 | [download-url]: https://npmjs.org/package/await-first 23 | 24 | ## Install 25 | 26 | ``` 27 | $ npm install await-first --save 28 | ``` 29 | 30 | ## Example 31 | 32 | - `awaitFirst(ee, events)` 33 | 34 | ```js 35 | const EventEmitter = require('events'); 36 | const awaitFirst = require('await-first'); 37 | 38 | async function waitMessageOrClose(ee) { 39 | const o = await awaitFirst(ee, [ 'message', 'close' ]); 40 | 41 | switch (o.event) { 42 | case 'message': 43 | const msg = o.args[0]; // [ 'hello world' ] 44 | console.log('new message =>', msg); 45 | break; 46 | case 'close': 47 | console.log('closed'); 48 | break; 49 | } 50 | } 51 | 52 | const ee = new EventEmitter(); 53 | waitMessageOrClose(ee); 54 | 55 | setTimeout(() => { 56 | ee.emit('message', 'hello world'); 57 | }, 1000); 58 | ``` 59 | 60 | - `obj.awaitFirst(events)` 61 | 62 | ```js 63 | const net = require('net'); 64 | const awaitFirst = require('await-first'); 65 | 66 | async function connect() { 67 | const socket = net.connect(8080, '127.0.0.1'); 68 | socket.awaitFirst = awaitFirst; 69 | 70 | try { 71 | // wait `connect` or `error` event 72 | await socket.awaitFirst([ 'connect', 'error' ]); 73 | } catch (err) { 74 | console.log(err); 75 | } 76 | // ... 77 | } 78 | 79 | connect(); 80 | ``` -------------------------------------------------------------------------------- /appveyor.yml: -------------------------------------------------------------------------------- 1 | environment: 2 | matrix: 3 | - nodejs_version: '6' 4 | - nodejs_version: '8' 5 | - nodejs_version: '9' 6 | 7 | install: 8 | - ps: Install-Product node $env:nodejs_version 9 | - npm i npminstall && node_modules\.bin\npminstall 10 | 11 | test_script: 12 | - node --version 13 | - npm --version 14 | - npm run test 15 | 16 | build: off 17 | -------------------------------------------------------------------------------- /lib/index.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const first = require('ee-first'); 4 | 5 | module.exports = function(emitter, events) { 6 | if (Array.isArray(emitter)) { 7 | events = emitter; 8 | emitter = this; 9 | } 10 | return new Promise((resolve, reject) => { 11 | first([ 12 | [ emitter ].concat(events), 13 | ], (err, ee, event, args) => { 14 | if (err) { 15 | reject(err); 16 | } else { 17 | resolve({ event, args }); 18 | } 19 | }); 20 | }); 21 | }; 22 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "await-first", 3 | "version": "1.0.0", 4 | "description": "Wait the first event in a set of event emitters and event pairs, then clean up after itself.", 5 | "main": "lib/index.js", 6 | "files": [ 7 | "lib" 8 | ], 9 | "scripts": { 10 | "autod": "autod", 11 | "pkgfiles": "egg-bin pkgfiles --check", 12 | "test": "npm run lint && npm run test-local", 13 | "test-local": "egg-bin test", 14 | "cov": "TEST_TIMEOUT=5000 egg-bin cov", 15 | "lint": "eslint . --ext .js", 16 | "ci": "npm run autod -- --check && npm run pkgfiles && npm run lint && npm run cov" 17 | }, 18 | "repository": { 19 | "type": "git", 20 | "url": "git+https://github.com/node-modules/await-first.git" 21 | }, 22 | "keywords": [ 23 | "ee-first", 24 | "await" 25 | ], 26 | "author": "gxcsoccer ", 27 | "license": "MIT", 28 | "bugs": { 29 | "url": "https://github.com/node-modules/await-first/issues" 30 | }, 31 | "homepage": "https://github.com/node-modules/await-first#readme", 32 | "dependencies": { 33 | "ee-first": "^1.1.1" 34 | }, 35 | "devDependencies": { 36 | "autod": "^3.0.1", 37 | "egg-bin": "^4.3.5", 38 | "egg-ci": "^1.8.0", 39 | "eslint": "^4.11.0", 40 | "eslint-config-egg": "^5.1.1", 41 | "mm": "^2.2.0" 42 | }, 43 | "ci": { 44 | "version": "6, 8, 9" 45 | }, 46 | "engines": { 47 | "node": ">= 6.0.0" 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /test/index.test.js: -------------------------------------------------------------------------------- 1 | 'use strict'; 2 | 3 | const assert = require('assert'); 4 | const awaitFirst = require('..'); 5 | const EventEmitter = require('events'); 6 | 7 | it('awaitFirst(ee, [ ... ])', function* () { 8 | const ee = new EventEmitter(); 9 | setTimeout(() => { ee.emit('foo', 'foo'); }, 200); 10 | setTimeout(() => { ee.emit('bar', 'bar'); }, 100); 11 | 12 | let triggered = false; 13 | ee.once('foo', () => { triggered = true; }); 14 | 15 | const o = yield awaitFirst(ee, [ 'foo', 'bar' ]); 16 | assert.deepEqual(o, { 17 | event: 'bar', 18 | args: [ 19 | 'bar', 20 | ], 21 | }); 22 | assert(triggered === false); 23 | assert(ee.listenerCount('foo') === 1); 24 | assert(ee.listenerCount('bar') === 0); 25 | }); 26 | 27 | it('awaitFirst(ee, [ "error", ... ])', function* () { 28 | const ee = new EventEmitter(); 29 | setTimeout(() => { ee.emit('foo', 'foo'); }, 200); 30 | setTimeout(() => { ee.emit('bar', 'bar'); }, 200); 31 | setTimeout(() => { ee.emit('error', new Error('mock error')); }, 100); 32 | 33 | try { 34 | yield awaitFirst(ee, [ 'foo', 'bar', 'error' ]); 35 | assert(false, 'should not run here'); 36 | } catch (err) { 37 | assert(err.message === 'mock error'); 38 | } 39 | assert(ee.listenerCount('foo') === 0); 40 | assert(ee.listenerCount('bar') === 0); 41 | assert(ee.listenerCount('error') === 0); 42 | }); 43 | 44 | it('obj.awaitFirst([ ... ])', function* () { 45 | const ee = new EventEmitter(); 46 | ee.awaitFirst = awaitFirst; 47 | setTimeout(() => { ee.emit('foo', 'foo'); }, 200); 48 | setTimeout(() => { ee.emit('bar', 'bar'); }, 100); 49 | 50 | let triggered = false; 51 | ee.once('foo', () => { triggered = true; }); 52 | 53 | const o = yield ee.awaitFirst([ 'foo', 'bar' ]); 54 | assert.deepEqual(o, { 55 | event: 'bar', 56 | args: [ 57 | 'bar', 58 | ], 59 | }); 60 | assert(triggered === false); 61 | assert(ee.listenerCount('foo') === 1); 62 | assert(ee.listenerCount('bar') === 0); 63 | }); 64 | 65 | it('obj.awaitFirst([ "error", ... ])', function* () { 66 | const ee = new EventEmitter(); 67 | ee.awaitFirst = awaitFirst; 68 | setTimeout(() => { ee.emit('foo', 'foo'); }, 200); 69 | setTimeout(() => { ee.emit('bar', 'bar'); }, 200); 70 | setTimeout(() => { ee.emit('error', new Error('mock error')); }, 100); 71 | 72 | try { 73 | yield awaitFirst(ee, [ 'foo', 'bar', 'error' ]); 74 | assert(false, 'should not run here'); 75 | } catch (err) { 76 | assert(err.message === 'mock error'); 77 | } 78 | assert(ee.listenerCount('foo') === 0); 79 | assert(ee.listenerCount('bar') === 0); 80 | assert(ee.listenerCount('error') === 0); 81 | }); 82 | --------------------------------------------------------------------------------