It's needed to wait until the function getFile() ends. I call this async function in other function. In "async function getFile()" is bug: "Unexpected token. A constructor, method, accessor, or property was expected."
import { LightningElement } from 'lwc';
export default class ShoppingCart extends LightningElement {
async function getFile() { // <= here is an error: "Unexpected token. A constructor, method, accessor, or property was expected."
let myPromise = new Promise(function(myResolve, myReject) {
//let req = new XMLHttpRequest();
//req.open('GET', "mycar.html");
//req.onload = function() {
// if (req.status == 200) {myResolve(req.response);}
// else {myResolve("File not Found");}
//};
//req.send();
});
console.log(await myPromise);
}
handlePlaceOrder() {
getFile();
}
In this example it's ok. What I have wrong? Is it possible call async function in other than non async function?
I tried also an advice from this. To use first Promise, then async, but there is the same error:
import { LightningElement } from 'lwc';
export default class ShoppingCart extends LightningElement {
function getFile() { // <= here is an error: "Unexpected token. A constructor, method, accessor, or property was expected."
let myPromise = new Promise(async function(myResolve, myReject) {
//let req = new XMLHttpRequest();
//req.open('GET', "mycar.html");
//req.onload = function() {
// if (req.status == 200) {myResolve(req.response);}
// else {myResolve("File not Found");}
//};
//req.send();
});
console.log(await myPromise);
}
handlePlaceOrder() {
getFile();
}
I use VSCode.