I have the following code (snippet out of a larger program and within an async function):
foo = questionData.media ? (await new Promise(resolve => {
const image = new Image();
image.onload = function () {
const canvas = document.ceateElement("canvas");
canvas.width = this.width;
canvas.height = this.height;
canvas.drawImage(image, 0, 0, this.width, this.height);
resolve(canvas.toDataURL("image/jpg"));
};
image.src = "https://example.com/" + questionData.media;
})) : false;
I keep getting SyntaxError: Unexpected token 'new'
on the first line, and I have no idea why. When I try to use parenthesis:
foo = questionData.media ? (await (new Promise(resolve => {
const image = new Image();
image.onload = function () {
const canvas = document.ceateElement("canvas");
canvas.width = this.width;
canvas.height = this.height;
canvas.drawImage(image, 0, 0, this.width, this.height);
resolve(canvas.toDataURL("image/jpg"));
};
image.src = "https://example.com/" + questionData.media;
}))) : false;
And I get Uncaught ReferenceError: await is not defined
.
Meanwhile, I have this code that works just fine:
data.push(await new Promise(resolve => {
const req = new XMLHttpRequest();
req.onreadystatechange = function () {
if (this.readyState === 4 && this.status === 200) {
resolve(this.responseText);
}
};
req.open("GET", `https://example.com/${id}/answer`, true);
req.send();
}));
What am I missing?