module.exports = function(fn, data){ var my_cb, my_res, error_cb, my_error 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 }, error: function(cb){ if (my_error) cb(my_error) else error_cb = cb } } return promise }