0

I have JQuery each statement that rolls on array of objects and checks if a value in that object is empty to let the user enter that value in a prompt window using bootbox then continue the loop till done.

after the each statement completes ajax function should fire holding the objects that the each refined.

problem is the ajax function fires up asynchronously during the each statement run time!

$.each(objs,function(k,v){

if (v == empty)
  //prompt bootbox modal to enter the value and (at this time ajax is fired
  v=bootboxpromptvalue   )

}) 
$.ajax({
..
...
....
data:objs
})  

Question is why ajax fires during the each runtime, since $.each statements supposed to be synchronous and anything after it should run after it is completes?

Suhayb
  • 3,163
  • 3
  • 23
  • 30
  • 4
    `$.each` is synchronous, but `$.ajax` is not. That is as it should be. – trincot Sep 09 '17 at 08:58
  • how to make the ajax runs right after the each so – Suhayb Sep 09 '17 at 08:59
  • Maybe you can fix your code: it has a loose `)`, and the `each` currently seems quite useless: redefining `v` is not going to alter the array. What is `bootboxpromptvalue`? Is it blocking? Probably not, so that means the user will only be prompted after all that code has finished running. – trincot Sep 09 '17 at 09:02
  • *how to make the ajax runs right after the each so?* - How to travel from europe to the US in 0 minutes? :/ – Jonas Wilms Sep 09 '17 at 09:03
  • Possible duplicate of [Sequencing ajax requests](https://stackoverflow.com/questions/3034874/sequencing-ajax-requests) – adiga Sep 09 '17 at 09:03
  • http://jsfiddle.net/1337/9TG8t/86/ – adiga Sep 09 '17 at 09:04
  • 2
    I think this problem has nothing to do with `ajax`, but with how `bootboxpromptvalue` is executed (possibly asynchronously), making it prompt after the `$.each` loop and `$.ajax` call have executed. – trincot Sep 09 '17 at 09:05
  • @trincot your guess is in place, bootbox prompt callbacks are async, any suggestion to have another prompting tool to feed the object with value during the eachruntime – Suhayb Sep 09 '17 at 09:13

1 Answers1

0

The bootbox prompt is asynchronous, and so before the user is prompted the $.each loop will just continue and the $.ajax call will execute.

To solve this, you should quit the execution as soon as you want to prompt the user for an answer, and only when you have the answer you should try the whole thing again:

function process() {
    var isEmpty = false;
    $.each(objs, function(k,v){
        isEmpty = v == empty;
        if (isEmpty) {
            bootbox.prompt("Enter value for " + k, function(answer) {
                objs[k] = answer;
                // Now we have an answer, try again.
                process();
            });
            return false; // break out of `each`
        }
    }
    // Exit if we asked for user input. There will be 
    // a new call to this function later.
    if (isEmpty) return;
    // We got everything now, so execute the request
    $.ajax({
        //...
        data: objs
    });
}
trincot
  • 317,000
  • 35
  • 244
  • 286