summaryrefslogtreecommitdiff
path: root/test/lib/promise.js
blob: c239f3066e6bbb2b8c22a47acb1e9b2b613c0df2 (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
module.exports = 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 promise = {
    then: function(cb){
      if (my_res) cb(my_res)
      else my_cb = cb
      return promise
    },
    error: function(cb){
      if (my_error) cb(my_error)
      else error_cb = cb
      return promise
    }
  }
  return promise
}