ahmet7661733
Posts: 0
Joined: Sun Jul 26, 2015 10:50 am

Execution order is not as expected.

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

Bob Fludder
Posts: 0
Joined: Fri Sep 14, 2012 1:09 am

Execution order is not as expected.

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

ahmet7661733
Posts: 0
Joined: Sun Jul 26, 2015 10:50 am

Execution order is not as expected.

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,

Pavel Zarudniy
Posts: 0
Joined: Mon Jul 06, 2015 8:56 am

Execution order is not as expected.

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

Return to “Issues”