last day (15 days later) » 

00:08
0
Q: Uncaught (in promise):How to await a function which returns a promise inside a loop?

Mr.AFI am working on awaiting a function that returns a promise inside a loop but i was not able to resolve it. the loop is for (var i = 0; i < rows.length; i++) { console.log("row " + i); getData(i).then(function (data) { console.log("data of row "+ i +" is "+ data); ...

user3482098
We need more information. What is exactly wrong here? What do you want the output to be? How come console.log("data of row "+ i +" is "+ data) is giving you output without the word is in it?
It should work fine.. post how you are defining your getData
you need not use .then and await together
user5734311
lemme put getData function
@SunilLama i did
@JThistle check update if you need more to put?
00:08
If getData, does not have the row, what would you expect to be printed out? Is it data of row false data?
@ChrisG i'v put a lot of codes here with good introduction
@user2340824 nothing have to printed but avoid deadlock
@Mr.AF what Chris meant was to create a plunker or fiddle for your issue, because, we cannot recreate it on our end, for eg the update that you did is missing whenAvailable..
@SunilLama is for when function read is ready to use that's all.
jsfiddle.net/te718Lsg/1, can you edit on this
@SunilLama looks great .does it work?
00:08
@Mr.AF, i want you to produce your issue in that fiddle...else i can post that as answer
@SunilLama post it as answer i am going to test it in production .
user5734311
Are you kidding...? That's the same as my fiddle.
@ChrisG sorry i thought you direct me to minimal reproducible example. why you didn't post it as answer btw i am working on that i am not sure working or not.
@ChrisG if it worked if you post it i will accept yours .
@ChrisG, i don't have to look at your fiddle to answer this, seriously...i have given here stackoverflow.com/questions/60517289/… as well, and most of the code is reused from the OP itself...
user5734311
@SunilLama Did I say that? I was just wondering why OP ignored my fiddle but they obviously missed the link.
user5734311
00:08
If that was all it took to solve this, I'm wondering what the real problem was? All I did was avoid a "await needs to be in an async function" error. I mean OPs code should produce that very error afaik, so I'm wondering where the Uncaught promise is coming from and what that deadlock business is all about? Anyway, I'm not going to post an answer here, so feel tree to pick whatever other one.
@ChrisG, the problem might be with chaining inside the getData itself, but cannot figure it out with what he has given so far
@SunilLama i did same as you but why it doesn't giving me same result? i will update the OP and what i did related to your answer.
@SunilLama see update.
user5734311
I guess your read() function doesn't return anything, so data is undefined. I'm mean you literally wrote reject(false) in there, and that's exactly what's happening. Why not call something like resolve({}) instead?
@ChrisG i am agree with you. sounds it's the main problem. lemme test your suggestion.
@ChrisG oh it worked , i replaced all reject(false) to resolve(undefined) now works even better than before. post it as the answer.

last day (15 days later) »