Using await and async to make load an parse Tau-Prolog before anything else

259 Views Asked by At

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

There are 1 best solutions below

0
José Antonio Riaza Valverde On

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');
        });
    });
}