condition-variable
v1.0.0
Published
An asynchronous condition variable for implemented internally using Promises
Downloads
6
Maintainers
Readme
condition-variable
An asynchronous condition variable for JavaScript implemented internally using Promises.
Install with:
npm install condition-variable
Usage Example
var CondVar = require('condition-variable');
var util = require('util');
var condition = new CondVar();
var maxTime = 10000; // 10 second maximum to wait for message
function notification(err, result) {
if (err) {
condition.cancel(err);
}
else {
condition.complete(result);
}
}
condition.wait(maxTime, (err, result) => {
if (err) {
console.log('FAILED: err=%s', util.inspect(err));
}
else {
console.log('SUCCESS: result=%s', util.inspect(result));
}
});
// TO SIMULATE COMPLETE WITH RESULT
// setTimeout(notification, 1000, null, 'IT WORKED!');
// TO SIMULATE AN ERROR
// setTimeout(notification, 1000, 'FAILED');