Last active
August 29, 2015 14:24
-
-
Save emadb/f4ed9a77aed634a04a15 to your computer and use it in GitHub Desktop.
Kata - synchronize asyncrounous results
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
| Consider to have a series of calculators functions that need to run in series. | |
| These calculators are asynchronous and any of those can return a value (a number >=0) or an error (-1). | |
| Calculators must be evaluated in order and you have to write a function that returns the value of the first calculator | |
| that doesn't fail. For first I mean the first in the chain of calculators. | |
| Test case | |
| function calculator1(resultCallback){ | |
| setTimeout(function(){ resultCallback(-1) }, 1000); | |
| } | |
| function calculator2(resultCallback){ | |
| setTimeout(function(){ resultCallback(2) }, 5000); | |
| } | |
| function calculator3(resultCallback){ | |
| setTimeout(function(){ resultCallback(3) }, 2000); | |
| } | |
| var calculators = [calculator1, calculator2, calculator3]; | |
| function runner(){ | |
| // ... it should return 2 (the result of calculator 2) | |
| } | |
| NOTE. | |
| The problem is that since every calculator is async you actually don't know wich one finish first. In the above case calcualtor2 will finish earlier that calculator1 but you have to choose the calculator1 since it is the first(given execution order) with a correct result |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment