-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtry_.js
More file actions
51 lines (51 loc) · 1.28 KB
/
try_.js
File metadata and controls
51 lines (51 loc) · 1.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
/* try_ - a natural control flow library for JavaScript
* Copyright by Adam Nemeth github.com/aadaam
* Licensed under Simplified BSD
*/
(function(scope){
scope.get_return_cb = function(args){
return function(result){
return args[0].call(args[1], null, result);
};
};
scope.get_throw_cb = function(args){
return function(err, result){
return args[0].call(args[1], err, result);
};
};
scope.try_ = function(obj,a_fn,ctx){
var truecallback = function(){};
var catch_fn = function(){};
var finally_fn = function(){};
var callback_fn = function(err, result){
if (!err){
truecallback.call(ctx ||this, result);
} else {
catch_fn.call(ctx ||this, err);
}
finally_fn.call(ctx || this);
};
var exec_fn = function(){
a_fn.call(obj, callback_fn, ctx || this);
};
var on_result = function(my_truecallback){
truecallback = my_truecallback;
return exec_fn;
};
exec_fn.catch_ = function(my_catch_fn){
catch_fn = my_catch_fn;
return exec_fn;
};
exec_fn.finally_ = function(my_finally_fn){
finally_fn = my_finally_fn;
return exec_fn;
};
return on_result;
};
})(window ? window : exports);
/*
try_(obj,async_fn)(function(result){
}).catch_(function(err){
}).finally_(function(){
})()
*/