Last active
December 20, 2017 03:40
-
-
Save leizongmin/739f6504af9eaaceac74798e3d8025bf to your computer and use it in GitHub Desktop.
兼容Promise的callback函数
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function createPromiseCallback() { | |
const callback = (err, ret) => { | |
if (err) { | |
callback.reject(err); | |
} else { | |
callback.resolve(ret); | |
} | |
}; | |
callback.promise = new Promise((resolve, reject) => { | |
callback.resolve = resolve; | |
callback.reject = reject; | |
}); | |
return callback; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function method(callback) { | |
callback = callback || createPromiseCallback(); | |
this._request(params, callback); | |
return callback.promise; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment