Last active
December 18, 2015 17:29
-
-
Save doughsay/5818720 to your computer and use it in GitHub Desktop.
Do many async operations and keep track of which ones fail and which ones succeed, using ff.
This file contains hidden or 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
| succeedAsync = (ms, cb) -> | |
| setTimeout -> | |
| console.log 'succeeding: ' + ms | |
| cb null, 'success: ' + ms | |
| , ms | |
| failAsync = (ms, cb) -> | |
| setTimeout -> | |
| console.log 'failing: ' + ms | |
| cb 'fail: ' + ms | |
| , ms | |
| randomInt = (min, max) -> Math.floor(Math.random() * (max - min + 1)) + min | |
| randomFn = -> [succeedAsync, failAsync][randomInt 0, 1] | |
| successes = [] | |
| failures = [] | |
| f = ff() | |
| f.next -> | |
| for i in [1..10] | |
| do -> | |
| done = f.wait() | |
| asyncFn = randomFn() | |
| ms = randomInt 100, 1000 | |
| asyncFn ms, (err, result) -> | |
| if err? | |
| failures.push err | |
| else | |
| successes.push result | |
| done() | |
| f.onComplete -> | |
| console.log | |
| successes: successes | |
| failures: failures |
This file contains hidden or 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
| succeeding: 160 | |
| succeeding: 319 | |
| succeeding: 325 | |
| failing: 584 | |
| failing: 593 | |
| failing: 624 | |
| failing: 639 | |
| succeeding: 892 | |
| succeeding: 935 | |
| failing: 964 | |
| { | |
| "successes": [ | |
| "success: 160", | |
| "success: 319", | |
| "success: 325", | |
| "success: 892", | |
| "success: 935" | |
| ], | |
| "failures": [ | |
| "fail: 584", | |
| "fail: 593", | |
| "fail: 624", | |
| "fail: 639", | |
| "fail: 964" | |
| ] | |
| } |
Author
Thanks for the reply, and sorry for my late response. I knew about groups when I wrote this, but unfortunately they don't solve this problem here. Groups work great if you only care about the successful async operations in the loop. FF will automatically fail if even just one of the grouped operations fails, even if the other pending operations are on-going and may succeed. You then end up with a failure message for a specific item, and no record of which ones succeeded.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You can use a "group". var group = f.group(); group.wait(); ... and then function(groupResult), which is an array of the slot results