plane icon Welcome to Microsoft Flight Simulator’s SDK Q&A Platform!

You have questions regarding the SDK? DevMode Tools? SimConnect? You would like to submit an idea for future improvements, seek help or exchange knowledge? You’re in the right place.

In the upcoming flighting, we've changed the behaviour of the content.xml file. If your addon uses this file, please read this article!

Please take a moment to read the platform’s guidelines before you get started!


question

ericmarciano avatar image
ericmarciano asked ericmarciano commented

JS Asynchronous Coeherent.call

Hello,

In the context of editing the flight plan, I have to add a list of waypoints to the flight plan, then I have to do some post-processing stuff after this is done.

To make things easy to understand, I can summarize my problem with the following piece of code:

for (let i=0; i < waypointList.length; i++) {
    Coherent.call("ADD_WAYPOINT", waypointList[i].icao, index, isActiveWpt).then(() => {
        // Do some stuff
    });
}
// Post-processing
// Do this stuff when all waypoints have been added

It works, all the waypoints are added to the flight plan as expected. But it doesn't work because my post-processing code executes BEFORE the waypoints are effectively added.

As far as I understand, this is because Coherent.call is asynchronous, I even think it uses multi-threading, so when I call it to add a waypoint, it register my request to do it later, when possible, and the JS code continues. Consequently, when the loop ends and the post-processing code executes, all the Coherent calls have not been finalized yet.

Is there a way to wait for the Coherent.call to be finished before continuing?

gauges
10 |10000

Up to 5 attachments (including images) can be used with a maximum of 4.8 MiB each and 23.8 MiB total.

hydrastik9488 avatar image
hydrastik9488 answered ericmarciano commented

Hey,

The best way to wait for Coherent.call to be finished is to put the code you want to be executed in the Promise handling (// Do some stuff). This code will be done after the Coherent.call is complete.

If you want to wait that all the waypoints have been loaded you can use the await keyword to wait the Promise of the Coherent call in an async function and the post-processing stuff code will wait that all promises are done.


function async addWaypoints() {
    for (let i=0; i < waypointList.length; i++) {
        await Coherent.call("ADD_WAYPOINT", waypointList[i].icao, index, isActiveWpt);
        // Do some stuff
    }
    // Post-processing
    // Do this stuff when all waypoints have been added
}

function caller() {
    addWaypoints().then(() => {
        // If you need to do some post-processing that was not done in addWaypoints 
    });
    // Be careful, stuff here will be executed even if the coherent calls are not finished
}


1 comment
10 |10000

Up to 5 attachments (including images) can be used with a maximum of 4.8 MiB each and 23.8 MiB total.

ericmarciano avatar image ericmarciano commented ·
Thank you, I will test this ASAP
0 Likes 0 ·
B21 avatar image
B21 answered ericmarciano commented

As a complete aside, I found it a lot easier to read the waypoints from the flight plan, create my own data structure with those and use that in my gauge code including any in-cockpit updates. The trade-off is whether you want the MSFS autopilot interpreting and updating the flightplan or not, and whether you want the MSFS representation of the flightplan on the map or your own drawing. It's great that MSFS allows both (I'm in the no-autopilot category) but it's painful that MSFS injects multiple additional waypoints into the flightplan before you get a chance to read the original waypoints.

1 comment
10 |10000

Up to 5 attachments (including images) can be used with a maximum of 4.8 MiB each and 23.8 MiB total.

ericmarciano avatar image ericmarciano commented ·
Yes, I thought about that, just like I was doing in the past with FSX and before, but here I would like to have the autopilot flying the route in NAV mode, which is why I want to use the MSFS flight plan.
0 Likes 0 ·

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 5 attachments (including images) can be used with a maximum of 19.1 MiB each and 23.8 MiB total.