Page 1 of 1

Execution order is not as expected.

Posted: Sun Jul 26, 2015 11:01 am
by ahmet7661733

Hi I'm doing the following in Javascript after a button click:

  1. do something (1),

  2. Execute the query

  3. do something (2)

    If I execute this javascript I see the following execution :

  4. do something (1)

  5. do something (2)

  6. Execute the query

    This is the code:

    itemToTest0='E120';

    alert('Before'); /(1)

    haram_query.execute({ / (2)
    data:{
    'where':{'Ingredient':itemToTest0} // here test first item
    },
    success:function(data){ // + length= 1
    if (data.length===1)
    {
    alert("Test1: ")
    }
    else
    {
    alert("Test2: ")
    notharam(itemToTest0); // Call to a function
    }
    },
    error:function ( jqXHR, textStatus, errorThrown ) {
    alert("error1!!!");
    }
    ,headers:{}
    });

    alert('after'); / (3)

    How can this be explained? Image


Execution order is not as expected.

Posted: Sun Jul 26, 2015 11:25 am
by Bob Fludder

Hi Ahmet,
I think you'll find its to do with the fact that the query is run asynchronously. You get the return in a callback but that will run whenever... The only way is to put the "do something 2" into the success callback after its done the "notharam" function - and don't forget you may need something more after the error bit too.

You could consider trying it with deferred/promises but thats another "interesting" discussion altogether.

If anyone out there knows how to do the sql synchronously please let us know !! I've been fighting with this for ages.

Bob


Execution order is not as expected.

Posted: Mon Jul 27, 2015 3:31 am
by ahmet7661733

Hi, BOB thanks for the Answer,

I Think you are right: It is asynchronous.
I have to wait for the comeback of the query,

Thanks,


Execution order is not as expected.

Posted: Mon Jul 27, 2015 7:14 pm
by Pavel Zarudniy

Hi ahmet,
Yes, you need to use success callback function to do actions after execute query