Awaitable Array
Utility Library for Creating Arrays that can be awaited
Uses:
- Writing efficient test cases
Usage
Waiting for Length
const AwaitableArray = ; // Wait until something is pushed to the arraylet arr = ; ; await arr; console; // [ 5 ]
Waiting for a Condition
const AwaitableArray = ; // Wait until something is pushed to the arraylet arr = ; let i = 0;let interval = ; // after 2500 msawait arr; console; // [ 1, 2, 3, 4, 5 ] ;
Create From Existing
const AwaitableArray = ;let arrExisting = 1 2 3 4 ; // Use the spread operator to expand the array to create a new AwaitableArraylet arr2 = ...arrExisting; // Resolves Immediately (next tick)await arr2; console; // [ 1, 2, 3, 4 ]
Padding Response Time
const AwaitableArray = ;// Can also pass in a padding (wait at LEAST this time) and a Timeoutlet arr3 = ; const padding = 10;const timeout = 50; // Defaults to 2000msawait arr3; // Waits 50ms // Wait at LEAST 10msconst arr4 = ...1 2 3; const padding = 10;await arr4; // Waits 10ms
Timeout Errors
// Timeout Errorconst arr5 = ...1 2; try await arr5; // Throws Promise.TimeoutError after 1 second catch e console; // Promise.TimeoutError (see http://bluebirdjs.com/docs/api/timeout.html)
Notes
Requires Node6 or Higher