using await in onReady()

Just wondering if someone can let me know if/what i’m doing wrong;

I have a simple backend function;

import wixSite from 'wix-site-backend';

export function getBusinessName() {
 return wixSite.generalInfo.getBusinessName();
}

wixSite.generalInfo.getBusinessName() returns a promise as we know from API doc.

In my front-end code I have the following;

import {getBusinessName} from 'backend/site';

$w.onReady(function () {
 // Sets the copyright text dynamically with current year and business name
 const currentDate = new Date();
 const businessName = await getBusinessName();

    $w("#textCopyright").text = "© " + currentDate.getFullYear().toString() + " by " + businessName + ".";
});

Now, this appears to work fine when previewed and published, however the editor tells me “Parsing error: Can not use keyword ‘await’ outside an async function”, and although everything looks fine I also get a error logged to the console in the browser;

public/pages/masterPage.js: await is a reserved word (8:22)
   6 | 	// Sets the current year
   7 | 	const currentDate = new Date();
>  8 | 	const businessName = await getBusinessName();
     | 	                     ^
   9 | 
  10 | 	$w("#textCopyright").text = "© " + currentDate.getFullYear().toString() + " by " + businessName + ".";
  11 | });

Is it to do with the way i’m assigning the variable value to await getBusinessName();?

When I implement this .then() I don’t have this problem… but my brain work synchronous :cry: so I like the await approach.

For completeness, .then() version below;

$w.onReady(function () {
  // Sets the copyright text dynamically with current year and business name
 const currentDate = new Date();
    getBusinessName()
    .then( (result) => {
      $w("#textCopyright").text = "© " + currentDate.getFullYear().toString() + " by " + result + ".";
    } );
});

Any help appreciated.

Cheers,

Jamie

$w.onReady(async function () { // Sets the copyright text dynamically with current year and business name const currentDate = new Date(); const businessName = await getBusinessName(); $w(" #textCopyright ").text = "© " + currentDate.getFullYear().toString() + " by " + businessName + “.”; });

make the onReady function an asynchronous function, Should work

thanks Scott, that did the trick!