function a(callback) {
var xhr, results, url;
url = 'http://example.com/search';
xhr = new XMLHttpRequest();
xhr.open('GET', url, true);
xhr.onload = function (e) {
if (this.status === 200) {
results = JSON.parse(this.responseText);
callback(results);
}
}
xhr.send();
}
function b(callback) {
var xhr, results, url;
url = 'http://example.com/search';
xhr = new XMLHttpRequest();
xhr.open('GET', url, true);
xhr.onload = function (e) {
if (this.status === 200) {
results = JSON.parse(this.responseText);
callback(results);
}
}
xhr.send();
}
function c(callback) {
var xhr, results, url;
url = 'http://example.com/search';
xhr = new XMLHttpRequest();
xhr.open('GET', url, true);
xhr.onload = function (e) {
if (this.status === 200) {
results = JSON.parse(this.responseText);
callback(results);
}
}
xhr.send();
}
a(function(){
// a成功之后,调用b
b(function(){
// b成功之后,调用c
c()
})
})
希望大概实现为需要直接在.then()后面接:
test().then(a)
.then(b)
.then(c)
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
你要做的事情是把callback形式的函数封装成Promise的形式