Skip to content

Instantly share code, notes, and snippets.

@dwelch2344
Created March 4, 2021 05:22
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Embed
What would you like to do?
test('events', async() => {
const { EventEmitter } = require('events')
const ee = new EventEmitter()
const handler = (breadcrumb: string, delay: number) =>
(promises: Promise<unknown>[]) => {
promises.push(new Promise(resolve => {
setTimeout(() => resolve(breadcrumb), delay)
}))
}
ee.on('thing', handler('a', 0) )
ee.on('thing', handler('b', 100) )
ee.on('thing', handler('c', 0) )
ee.on('thing', handler('d', 3 * 1000) )
const promises: Promise<unknown>[] = []
ee.emit('thing', promises)
console.log({promises}, promises[0] instanceof Promise)
const results = await Promise.all(promises)
console.log({results})
})
@dwelch2344
Copy link
Author

  console.log
    {
      promises: [
        Promise { <pending> },
        Promise { <pending> },
        Promise { <pending> },
        Promise { <pending> }
      ]
    } true
    
    
      console.log
    { results: [ 'a', 'b', 'c', 'd' ] }
    ```

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment