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 }