Created
August 27, 2014 22:04
-
-
Save fitzgen/8e2f13e13ed6f854d823 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Cu.import("resource://gre/modules/Task.jsm") | |
let runTaskWhileValid = (isValid, generator) => { | |
Task.spawn(function* () { | |
const g = generator(); | |
let value, resolved; | |
let done = false; | |
while (!done) { | |
try { | |
({ value, done }) = g.next(resolved); | |
resolved = yield value; | |
} catch (e) { | |
g.throw(e); | |
} | |
if (!isValid()) { | |
done = true; | |
resolved = undefined; | |
console.log(done) | |
} | |
} | |
return resolved; | |
}); | |
}; | |
let getRandomIn10ms = () => new Promise(resolve => setTimeout(() => resolve(Math.random()), 10)); | |
let start = Date.now(); | |
let lessThan50ms = () => Date.now() - start < 50; | |
runTaskWhileValid(lessThan50ms, function* () { | |
let i = 0; | |
while (true) { | |
console.log("iteration number ", i++); | |
let rand = yield getRandomIn10ms(); | |
console.log("rand = ", rand); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment