Skip to content

Instantly share code, notes, and snippets.

@emadb emadb/async kata
Last active Aug 29, 2015

Embed
What would you like to do?
Kata - synchronize asyncrounous results
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
You can’t perform that action at this time.