是否可以等到“Fetch”指令完成后再执行下一个代码/指令??(就像 AJAX 等待的工作方式一样)
这些函数实际上用于从 Facebook Graph API 请求帖子的“隐私值”,但是,我如何运行警报提示框直到“一切”结束(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
});
Run Code Online (Sandbox Code Playgroud)
_
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}` )
});
}
Run Code Online (Sandbox Code Playgroud)
_
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(); …
Run Code Online (Sandbox Code Playgroud)