How to make the code wait for another function to complete?

I have read up on promises and async/await and I will be honest I don’t get it ( I come from a VBA coding background I am just 2 weeks into my javascripting journey ). Please help me figure out the solution and then I can review it and hopefully understand its use case for future use. For simplicity sake I will post the relevant portions of the code.

Function A ( Note: these lines are within a nested if statement. The problematic line is #2, the one highlight in bold red. The called function is not completing in time and it results in an undefined value in the collection after insert. )
line1 : newassignmentday = getRandomValue( 1 , 5 )
line2: newAssignment = newPOI();
line3: console.log( 'New Assignment: ’ + newAssignment);
line4: departuretime = ‘08:00’ ;

Function B - has multiple wixData.Query and wixData.Update actions hence the increased time.

I tried the following to resolve it within function A . This will show my ignorance but I am sure I will learn from it.

First attempt: set function A to async. and update line2 as follows: newAssignment = await newPOI();. Wix didn’t like the await statement there. It said : " Cannot use keyword ‘await’ outside an async function". I use to have this issue with declaring variables but learnt to declare them before the If statement. But I didn’t think this applies here. How would you declare the await command? But I could we way off on that.

Second attempt : change line2 as follows: newAssignment = Promise.resolve(new POI());. Wix was OK with that but it didn’t solve the problem.

So, this is where you lovely folks and experts of Wix come in. What can I do to make the code wait for that line to complete before proceeding?

Thanks in adavance.

How to use Async-Aawait & Co. …
https://www.wix.com/velo/forum/tips-tutorials-examples/promises-promises

Normal function:

import wixUsers from 'wix-users';

$w.onReady(function () {
    myFunction() 
});

function myFunction() {
   let user = wixUsers.currentUser;
   let eMail = user.getEmail()
   console.log(eMail)
}

Result —>

Async-function:

import wixUsers from 'wix-users';

$w.onReady(function () {
    myFunction() 
});

async function myFunction() {
   let user = wixUsers.currentUser;
   let eMail = await user.getEmail()
   console.log(eMail)
}

Result —> fetched eMail like —> xxxxx.xxxxx@xxxx.com

Or you can do the same in another way of coding by using —> .then()

thank you for responding @russian-dima. However, I tried that and this is what I got:

First attempt: set function A to async. and update line2 as follows: newAssignment = await newPOI();. Wix didn’t like the await statement there. It said : “Cannot use keyword ‘await’ outside an async function”. I use to have this issue with declaring variables but learnt to declare them before the If statement. But I didn’t think this applies here. How would you declare the await command? But I could we way off on that.

Hi Othniel.

Can u share the code u using?
Its easyer to see what you are trying to do so we can provide better information.

Kind regards,
Kristof.

hi @Kristof here they. The only problematic call is the newPOI one.

function createSchedule(){
$w( “#text1” ).text = ‘Processing…’ ;
wixData.query( ‘pilotschedule’ )
.descending( ‘scheduledate’ )
.find()
.then ((results) => {
let lastschedule = results.items[ 0 ];
var fltdate = lastschedule.scheduledate;
var assignmentday = lastschedule.assignmentday;
var assignment = lastschedule.assignment;
var newfltdate = getNewDate(fltdate)
let newAssignment;
let departuretime;
var newassignmentday
if (assignmentday> 1 ){
newassignmentday = assignmentday - 1
if (assignment !== ‘WKNA’ && assignment !== ‘WKEU’ ){
newAssignment = newPOI();
console.log( 'New Assignment: ’ + newAssignment);
departuretime = ‘08:00’ ;
}
else {
newAssignment = assignment;
departuretime = getDepartureTime();
}
}
else {
if (assignment !== ‘WKNA’ && assignment !== ‘WKEU’ ){
newassignmentday = getRandomValue( 1 , 5 )
newAssignment = newPOI();
console.log( 'New Assignment: ’ + newAssignment);
departuretime = ‘08:00’ ;
}
else {
newassignmentday = 5
departuretime = getDepartureTime()
wixData.query( ‘pilots’ )
.find()
.then ((results1) => {
let pilotrecord = results1.items[ 0 ];
let lastworklocation = pilotrecord.lastworklocation;
if (lastworklocation === ‘WKNA’ ){
newAssignment = ‘WKEU’ ;
}
else if (lastworklocation === ‘WKEU’ ){
newAssignment = ‘WKNA’
}
})
}
}
let flightschedule = {
“scheduledate” : newfltdate,
“assignment” : newAssignment,
“assignmentday” : newassignmentday,
“departuretime” : departuretime
}
insertFlightSchedule(flightschedule);
})

}

function newPOI (){
wixData.query( ‘pointsofinterest’ )
.descending( ‘order’ )
.find()
.then ((results) => {
let firstpoi = results.items[ 0 ];
var lastpoiid = firstpoi.order;
var selectedpoi = getRandomValue( 1 , lastpoiid);
console.log( 'Total POI: ’ + lastpoiid + ’ Random POI: ’ + selectedpoi)
wixData.query( ‘pointsofinterest’ )
.eq( ‘order’ ,selectedpoi)
.find()
.then ((results1) => {
let thepoirecord = results1.items[ 0 ];
let poiicao = thepoirecord.icao;
let poiupdate = {
“order” : null ,
“status” : ‘PLND’
}
let poiv2 = Object.assign(thepoirecord,poiupdate);
wixData.update( ‘pointsofinterest’ , poiv2)
console.log( 'Random POI: ’ + poiicao)
return poiicao
})
})
}

I figured it out by chaining my .then statements whenever I invoke a wixData command.

Well done.

Update: Today I figured out how to use async/await with wixdata.xxx.

So much simpler.

Somebody should tell those YouTubers out there. They are doing a horrible job of explaining it. I looked at over 30 videos with none just going right at it.

Anyways, I am a happy camper now.