3

my task is to load and parse Tau-Prolog code in the Browser before anything else will be executed. I tried this approach (webProlog.pl contains Tau-Prolog code):

var session = pl.create(1000); 
async function init_prolog() {
    // load tau
     await $.get("/web/webProlog.pl", function(data) {
        parsed = session.consult(data);
        session.query("init.");
        session.answer(printAnswer); // needed for triggering query
    });

    console.log('Prolog init done');
}

Inside the "init" query there is a log message "Tau-Prolog init done". If I don't use await/asnyc, the message "Prolog Init done" comes before the Tau-Prolog message, with the code above the sequence is correct (first Tau Prolog message, then Prolog init done).

The question is: I'm not an JS expert. Would this work with all common browsers, are there side effects or disadvantages I cannot see by this approach? Are there better solutions?

The overall code would continue with PixiJS stuff setup.

Cheers and thanks for any hint

Hans

  • 1
    An `await` will not *stop* other code from executing. Quite the opposite `await` will only pause the current function until the next thing (in this case `$.get()`) is resolved. This pause is done so that *other code* can execute in the meantime . – VLAZ Apr 09 '20 at 09:50
  • [it will work](https://caniuse.com/#search=await) everywhere, except... IE – CapelliC Apr 09 '20 at 12:02
  • Now I could observe that what you described with the await behaviour. In the following code, there is another query in Tau Prolog with sometimes fail. The reason is indeed because $.get() will wait but not execution overall. So.... the problem remains, how can I stop overall execution until $.get is finished? – Hans Nikolaus Beck Apr 27 '20 at 11:14

1 Answers1

1

You must execute your code as a callback of the answer method:

var session = pl.create(1000); 
function init_prolog() {
    // load tau
    $.get("/web/webProlog.pl", function(data) {
        parsed = session.consult(data);
        session.query("init.");
        session.answer(function(answer) {
            printAnswer(answer);
            console.log('Prolog init done');
        });
    });
}