multiple actions in eval() on safari asynchronous

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • blainefox
    New Member
    • Mar 2017
    • 1

    multiple actions in eval() on safari asynchronous

    First I have a script file like:

    alert('a');
    if(***) alter('b');

    saved on the server.

    I'm using AngularJS developing a mobile app which reads and executes it using eval().

    On iOS safari alter('b') will come up first then alter('a') and it seems codes after eval() will run asynchronously.

    However on Android & PC Chrome the sequence is as expected as alter('a')->alter('b')->things after eval().

    How can I run run scripts synchronously on Safari? Many thanks.
  • gits
    Recognized Expert Moderator Expert
    • May 2007
    • 5388

    #2
    i am not aware of such behaviour of eval but 2 things - 1st: using eval is pretty much very bad practice - i've never seen a reason to use it except bad design of a software or (in the old days) evaluating a JSON response from a XMLHttpRequest. 2nd: if something is async there is usually 2 solutions - you can use a timeout which is very bad practice as well - or refactor everything so that the code uses callbacks whenever it should proceed with the next step - which would be the preferable solution. ES6 provides promises that can be used to achieve the same goal - and basically transpile into an appropriate callback-chain.
    Last edited by gits; Mar 16 '17, 09:25 AM.

    Comment

    Working...