问题:
I want to work with promises but I have a callback API in a format like: 我想使用Promise,但是我有一个类似以下格式的回调API:
1. DOM load or other one time event: 1. DOM加载或其他一次事件:
window.onload; // set to callback
...
window.onload = function() {
};
2. Plain callback: 2.普通回调:
function request(onChangeHandler) {
...
}
request(function() {
// change happened
...
});
3. Node style callback ("nodeback"): 3.节点样式回调(“ nodeback”):
function getStuff(dat, callback) {
...
}
getStuff("dataParam", function(err, data) {
...
})
4. A whole library with node style callbacks: 4.带有节点样式回调的整个库:
API;
API.one(function(err, data) {
API.two(function(err, data2) {
API.three(function(err, data3) {
...
});
});
});
How do I work with the API in promises, how do I "promisify" it? 如何在promise中使用API,如何“承诺”它?
解决方案:
参考一: https://stackoom.com/question/1WUQK/如何将现有的回调API转换为Promise参考二: https://oldbug.net/q/1WUQK/How-do-I-convert-an-existing-callback-API-to-promises
来源:oschina
链接:https://my.oschina.net/stackoom/blog/4339498