I read some example how to refactor the code to fit the style of promise but it seems they may need some help from other promise libraries.
My issue is that I have multiple async tasks which some of them may rely on some information from another call. So my code looks like that:
Let's say I have a Library table. A Library table has many Books. A Book has many Pages. A Page has many photos.
I am also using LoopBack to create the instance for these tables.
I have an array of library objects in this format.
{
"location": "Hong Kong Island",
"size": "25sq meter",
"availableBooks" : [
{
"bookName": "Harry Potter",
"pages": 235,
"author": "J R Rowling"
"pages": [
{
"page": 1,
"content": "XXX",
"photos": [
{
"urlLink": "https://someLink",
"size": "360X250"
},
{
"urlLink": "https://google",
"size": "650X250"
}
]
}
,
{
"page": 2,
"content": "XXX"
}
]
},
{
"bookName": "Lord Of The Rings",
"pages": 335,
"author": "J. R. R. Tolkien"
}
]
}
For pseudo code, it looks like this.
for(something){
asyncCallA()
.then((A) => {
for(something){
asyncCallB(A)
.then((B) => {
for(something){
asyncCallC(B)
.then((C) => {
for(something){
asyncCallD(C)
}
})
}
})
}
})
}
For common cases, I understand how to use promise to chain the async operations. But in this case where the async calls are dependent to each other and invovle a lot of for loop, I have no idea how to flatten the call. Can anyone please provide some insights? Thanks.