co-suspend
Suspend execution until it is resumed asynchronously.
Installation
$ npm install co-suspend
Example 1
Pause program execution until resume, using yield
.
var request = ;var suspend = ; moduleexports { var marker = ; ; return marker; // return body};
Note: If a non-null first argument is passed to marker.resume()
, it must be an Error.
Any other argument will be ignored.
Example 2
Use co-suspend
instead of thunkify
.
var fs = ;var suspend = ; { var marker = ; fs; return marker;}
Example 3
Wait a certain amount of time (milliseconds) before timing out.
var db = ;var suspend = ; { var marker = ; db; return marker; // wait 3 seconds}
The above code will wait at most 3 seconds, after which an error will be thrown if
marker.resume()
has not been called.
var connect; try connect = ; catch e console;
Example 4
Markers are reusable, too!
var marker = ;var result; ;result = marker;//.... ;result = marker;//... ;result = marker;//...
Example 5
Using an experimental feature to enqueue some yieldable in the waiting marker.
var marker = ;var result; var q = db;q; result = marker;
Note: calling marker.enqueue()
to an un-waiting marker will simply execute
the yieldable (function, generator, thunk, etc.) inside an anonymous co
context.
Note: because marker.enqueue()
is meant to be used inside a synchronous
context, the function does not return anything. In fact, there is really no reason
why it should return anything.
License
MIT