Anyone who studies the front end should have heard of callback hell, so what is callback hell? How to solve it?
Generally, there are two types of callback hell:
The first:
getData({
url:'http://localhost:3000/get',
success:(data)=>{
getData({
url:'http://localhost:3000/getBooks?id=1',
success:data=>{
console.log(data)
getData({
url:'http://localhost:3000/getNews?id='+data[0].id,
success:data=>{
console.log(data)
}
})
}
})
}})
The second type:
getDataPromise('http://localhost:3000/get').then(data => {
return getDataPromise('http://localhost:3000/getBooks?id='+data[0].id)
}).then(data => {
return getDataPromise('http://localhost:3000/getNews?id='+data[0].id)
}).then(data => {
console.log(data)
})
Solution:
async function get(){
const res1 = await getDataPromise('http://localhost:3000/get')
const res2 = await getDataPromise('http://localhosr:3000/getBooks?id='+res1[0].id)
const res3 = await getDataPromise('http://localhosr:3000/getNews?id='+res2[0].id)
console.log(res3)
}
get()