Skip to content

Instantly share code, notes, and snippets.

@bjouhier
Last active August 29, 2015 14:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bjouhier/11234661 to your computer and use it in GitHub Desktop.
Save bjouhier/11234661 to your computer and use it in GitHub Desktop.
waitForOne.js
var galaxy = require('galaxy');
function *waitForOne(futures) {
return yield galaxy.star(function(cb) {
for (var i = 0; i < futures.length; i++) {
galaxy.unstar(futures[i], 0)(function(e, r) {
if (cb) cb(e, r);
cb = null;
});
}
}, 0)();
}
var sleep = galaxy.star(function(ms, cb) {
setTimeout(cb, ms);
}, 1);
function *delay(ms, result) {
yield sleep(ms);
return result;
}
function *test() {
var futs = [10, 4, 2, 6].map(function(i) {
return galaxy.spin(delay(i * 100, i));
});
var result = yield waitForOne(futs);
console.log("result=" + result);
}
galaxy.unstar(test)(function(err) {
if (err) throw err;
});
"use strict";
function waitForOne(cb, futures) {
for (var i = 0; i < futures.length; i++) {
futures[i](function(e, r) {
if (cb) cb(e, r);
cb = null;
});
}
}
function delay(_, ms, result) {
setTimeout(_, ms);
return result;
}
function test(_) {
var futs = [10, 4, 2, 6].map(function(i) {
return delay(!_, i * 1000, i);
});
var result = waitForOne(_, futs);
console.log("result=" + result);
}
test(function(err) {
if (err) throw err;
});
@bjouhier
Copy link
Author

bjouhier commented Jul 4, 2014

added waitForOne._js: same example, but written with streamline.js

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