【发布时间】:2020-07-08 08:35:08
【问题描述】:
下面的 arr 变量是相似数组的集合。
function site1(url){
return axios.get(url)
.then((response)=>{
if(response.status==200){
//code for getting data
//arr=[]---------initial value (globally declared variable)
for(i=1;i<=limit;i++){
arr.push(someData);
//after this push arr=[[item1,item2,item3,item4,item5]]
//arr is an collection of similar arrays
//there are many links one for each array inside arr
//-----operation here to get someLink-------
for(a=0;a<arr.length;a++){
someOtherSite(somelink,arr).then((data)=>{
arr=data;
});
}
}
}
return arr;
})
}
function someOtherSite(url,arr){
return axios.get(url)
.then(response=>{
//get some dataItem
arr[arr.length-1].push(dataItem);
return arr;
})
}
函数调用:
site1(url).then(data=>{
console.log(data);
})
我希望 arr 的值是这样的: arr=[[item1,item2,item3,item4,item5,dataItem],.......] 但是返回发生在之前,并且 arr 的值在没有 dataItem 的情况下显示。 如何让值的返回等待并在过程完成后发生。
【问题讨论】:
-
你做错了。相反,您应该只返回 Axios get 并将成功和错误处理程序作为回调传递。
标签: javascript node.js axios