32 lines
536 B
JavaScript
32 lines
536 B
JavaScript
|
return {
|
||
|
execute: execute,
|
||
|
tryEach: tryEach,
|
||
|
map: map
|
||
|
}
|
||
|
|
||
|
function tryEach(strategies) {
|
||
|
return function() {
|
||
|
for(var i = 0; i < strategies.length; i++) {
|
||
|
var result = strategies[i]();
|
||
|
if(result != undefined) {
|
||
|
return result;
|
||
|
}
|
||
|
}
|
||
|
};
|
||
|
}
|
||
|
|
||
|
function execute(strategy, onSuccess, onError) {
|
||
|
var result = strategy();
|
||
|
if(result != undefined) {
|
||
|
return onSuccess(result);
|
||
|
} else {
|
||
|
return onError(result);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
function map(f, strategy) {
|
||
|
return function() {
|
||
|
return execute(strategy, f, function() {return;});
|
||
|
};
|
||
|
}
|