last day (14 days later) » 

2:16 AM
1
Q: wait for the function to finish before continuing to the next loop in a $.each function

Ahmed MujtabaI'm not an expert in JS or jQuery by any means. I'm trying to write code for a chat app that does something like this: Get conversation list from an AJAX call Populate the left pane of the web page with the conversations returned by the API Bind the chat list items to the right side of the pan...

 
getUser function is async. I have updated my question with the code.
 
@AhmedMujtaba No value is returned from getUser call.
 
@guest271314 what is it supposed to return? the callback function returns the data from the Ajax response.
 
No value is actually returned from the getUser function. You could probably use .queue(), $.map() to return results in sequential order, with next function being called only when current asynchronous process completes. It also appears that .done() is chained to $.each()? Though $.each() does not actually return a value either.
 
but what about the call back that is called once the getUser function get's the response from the Ajax call? Because if I'm not returning anything then how am I able to use the value that I get from the Ajax function inside the function that calls getUser method? also does it really matter that I return something in the .each? because the idea is to execute the next sequence of code when the $.each loop finishes. I can use the data value in the chained function as well. If I was using 'then' instead of 'done' then I would have to retur something. Sorry if I sound dumb but please do correct me
 
2:16 AM
Do not believe it is possible to return a value from $.each(). You need to return a value from getUser or within .done(). Alternatively, you could use $.map(), .queue(), .promise(); see Answer below
 
ohh no I don't mean return value from $.each() but the function inside which $.each exists. I'm having a look at your code. Thanks :)
 
hello there
thanks for helping me out
 
No worries.
 
I was just going through jquery documentation to get more explanation about .map and other functions u used in ur code
I just needed to clarify a few things with u
 
2:21 AM
Which portions of javascript at Answer are you referencing?
 
sorry i have a crappy internet. It keeps disconnecting
 
$.map() iterates similarly to $.each(), though returns an Array as value
 
okay here's your processQueue function
function processQueue (data) {
return $({}).queue("ajax", $.map(data, function(id) {
return function(next) {
return getUser(id, function(val) {
// do stuff when asynchronous function returns a value
return $("<div>", {html:"id:" + id + ", value:" + val * Math.PI})
.appendTo("body").after("<br>");
}, next) // pass `next` function
}
})).dequeue("ajax").promise("ajax")
}
here, data is my conversation list which I'm getting from the Ajax call in the function before the function in which .$each() loop is right?
where are u getting the value of 'val'?
what is val in this context
 
Yes. $.map() iterates collections similarly to $.each(). Though $.map() returns an array as value. At javascript at Answer a function is pushed to "ajax" queue for each item in data. next is an alias for $.dequeue(), which calls next function in queueName, i.e., "ajax" when next() is invoked.
 
ohh i get it
 
2:33 AM
val is return value from asynchronous process; e.g., function(val){} portion of$.ajax().then(function(val){}), passed as callback to getUser, attempting to follow the pattern at original Question.
 
what do these 2 lines do
.dequeue("ajax").promise("ajax")
"At javascript at Answer a function is pushed to "ajax" queue for each item in data." I didn't get this
maybe u mistyped
 
a) .dequeue("ajax") calls first function in "ajax" queue array set at $({}).queue("ajax", $.map()). b) .promise("ajax")` returns the jQuery promise object associated with the "ajax" queue array is empty.
You are correct. "pushed" is not correct term. Actually, returned from $.map() call
 
ohh got it
Thank you so much. I'm gonna read a bit more about .queue and dequeue functions to understand things better though ur explanation helped a lot
 

last day (14 days later) »