Web Fetch API(等待FETCH完成,然后执行下一条指令)
是否可以等到‘FETCH’指令完成后再执行下一个代码/指令?(就像AJAX等待的工作方式一样)
这些函数实际上是用来从Facebook Graph API请求POST的‘隐私值’,但是,我如何运行警报提示框,直到"一切"结束(FirstRequestToGraph+RequestNextPage)
FirstRequestToGraph(AccessToken)
.then(function() {
RequestNextPage(NextPage); //recursively until there's no more next page
})
.then(function() {
alert("everything have ended nieely"); //still pop up before RequestNextPage Completed
});
_
function RequestNextPage(NextPage){
fetch(NextPage, {
method: 'GET'
})
.then(function(response) {
return response.json();
})
.then(function(json) {
if(json.data.length == 0 ){
console.log("ended liao lur");
}else{
RequestNextPage(json.paging.next);
}
})
.catch(function(err) {
console.log(`Error: ${err}` )
});
}
_
function FirstRequestToGraph(AccessToken){
fetch('https://graph.facebook.com/v2.8/me?fields=posts.limit(275){privacy}%2Cname&access_token='+AccessToken, {
method: 'GET'
})
.then(function(response) {
return response.json();
})
.then(function(json){
NextPage = json.posts.paging.next;
})
.catch(function(err) {
console.log(`Error: ${err}` )
});
}
解决方案
FirstRequestToGraph(AccessToken).then(function() {
alert('testing1234');
});
function RequestNextPage(NextPage) {
return fetch(NextPage, {
method: 'GET'
})
.then(function(response) {
return response.json();
})
.then(function(json) {
RequestNextPage(json.paging.next);
})
.catch(function(err) {
console.log(`Error: ${err}`)
});
}
function FirstRequestToGraph(AccessToken) {
return fetch('https://graph.facebook.com/v2.8/me?fields=posts.limit(275){privacy}%2Cname&access_token=' + AccessToken, {
method: 'GET'
})
.then(function(response) {
return response.json();
})
.then(function(json) {
if(json.data.length !== 0 ){
return RequestNextPage(json.paging.next);
}
})
.catch(function(err) {
console.log(`Error: ${err}`)
});
}
相关文章