blob: f458ab06e843628ed26998678bd9fedca7356a80 (
plain)
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
|
var promise = function(fn, data){
var my_cb, my_res, error_cb, my_error
data = data || {}
data.success = function(res){
my_res = res
if (my_cb) {
my_cb(res)
}
}
data.error = function(res){
my_error = res
if (error_cb) {
error_cb(res)
}
else {
console.log('error!')
console.log(res)
}
}
fn(data)
var p = {
then: function(cb){
if (my_res) cb(my_res)
else my_cb = cb
return p
},
error: function(cb){
if (my_error) cb(my_error)
else error_cb = cb
return p
}
}
return p
}
|