You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Running two promise with identic query somehow went wrong #1368
Problem
I have a google spreadsheet and want to load several sheets in paralel. But the result is not working as expected.
Imagine the google sheet have 2 sheets, categories and products.
asyncfunctionloadCategory(){consturl='https://docs.google.com/spreadsheets/d/1iN8IsyPAJe8nFlBUvjEu1UX6mvxOORLIaG8CIyHkFGU/gviz/tq?tqx=out:csv&gid=976108113';constres=awaitalasql.promise('SELECT * FROM CSV(?, {headers:true})',[url]);console.log('category',res[0]);returnres;}asyncfunctionloadProduct(){consturl='https://docs.google.com/spreadsheets/d/1iN8IsyPAJe8nFlBUvjEu1UX6mvxOORLIaG8CIyHkFGU/gviz/tq?tqx=out:csv&gid=1160488713';constres=awaitalasql.promise('SELECT * FROM CSV(?, {headers:true})',[url]);console.log('product',res[0]);returnres;}loadCategory();loadProduct();
I'm expecting the console log would be like this
But I got
which the result of category goes to loadProduct and no response from loadCategory
Please notice that the query are identic both in loadProduct and load Category, only the parameters are different
constres=awaitalasql.promise('SELECT * FROM CSV(?, {headers:true})',[url]);
When I change the query so they are not identic, e.g adding alias
asyncfunctionloadCategoryWithAlias(){constres=awaitalasql.promise('SELECT * FROM CSV(?, {headers:true}) AS `Category`',[url]);
...
}asyncfunctionloadProductWithAlias(){constres=awaitalasql.promise('SELECT * FROM CSV(?, {headers:true}) AS `Product`',[url]);
...
}
Your async function never returns because the scripts reaches the end and kills the process.
You need to await the answer.
To await the answer the need to run it inside a function. The following should do the trick, but I did not test it
(async()=>{asyncfunctionloadCategory(){consturl='https://docs.google.com/spreadsheets/d/1iN8IsyPAJe8nFlBUvjEu1UX6mvxOORLIaG8CIyHkFGU/gviz/tq?tqx=out:csv&gid=976108113';constres=awaitalasql.promise('SELECT * FROM CSV(?, {headers:true})',[url]);console.log('category',res[0]);returnres;}asyncfunctionloadProduct(){consturl='https://docs.google.com/spreadsheets/d/1iN8IsyPAJe8nFlBUvjEu1UX6mvxOORLIaG8CIyHkFGU/gviz/tq?tqx=out:csv&gid=1160488713';constres=awaitalasql.promise('SELECT * FROM CSV(?, {headers:true})',[url]);console.log('product',res[0]);returnres;}awaitloadCategory();awaitloadProduct();})()
or you might want to do it a bit faster by initiating the requests with a await Promise.all(loadCategory(), loadProduct())
Activity
mathiasrw commentedon Oct 1, 2021
Your async function never returns because the scripts reaches the end and kills the process.
You need to await the answer.
To await the answer the need to run it inside a function. The following should do the trick, but I did not test it
or you might want to do it a bit faster by initiating the requests with a
await Promise.all(loadCategory(), loadProduct())Let me know if it works.
niwasmala commentedon Nov 19, 2021
Sorry for the late reply. Yep, it works! My bad for missing the promise and not waiting for the answer.
Thanks! 👍