now there is such a requirement, for example, I have an array:
arr=[
{
id:"84001",
name:""
},{
id:"84002",
name:""
},{
id:"84003",
name:""
},{
id:"84004",
name:""
},
]
I want to get the url, of all the music by calling API through the id in the array. If I call ajax through the traversal array, I get the Url? of the music.
var urlList=[]
arr.forEach((item)=>{
$.ajax({
url:"api/v1/getMusicById?id="+item.id,
type:"get",
success:(res)=>{
urlList.push(res.url)
}
})
})
console.log(urlList)
this is what I thought at first, but there must be something wrong with this. The ajax request is asynchronous
I want to complete the ajax request after the traversal is completed, and then I can get a url list
how can you implement it?