Promisified, asynchronous ES6 for..of
Installation
npm install async-for-of
Usage
async-for-of can loop asynchronously over any iterable, only requesting the item when "next" is called
var forOf =
iterable is an array, generator, function or custom iterable of any type (typically Promises)
forOf returns a Promise that is fulfilled when:
- end is called (with resolve with the value passed to that function)
- the iterable returns done (will resolve with an undefined result)
Check out the tests to see expected behaviour.
Examples
Asynchronous Random Number Generator
This example asynchronously generates random numbers and resolves when the result matches certain criteria
var { console return Promise}
Asynchronous Random Number Generator #2
This builds on the above example, using an ES6 generator to add some control flow - in this case, enforcing the maximum number of attempts
var { var maxAttempts = 5 for var i = 0; i < maxAttempts; i++ console Promise throw 'Max Attempts Exceeded'}
Peer Discovery
This example finds an active peer from a list (ideally broadcasted) and resolves with a socket
var server = net;server; var peers = host: '127.0.0.1' port: 8000 host: '127.0.0.1' port: 8001 host: '127.0.0.1' port: 8002