2.6.2 Async / Await
async function returnTheAnswer() {
return 42;
}
const implicitlyReturnedPromise = returnTheAnswer();
console.log(implicitlyReturnedPromise instanceof Promise); // true
implicitlyReturnedPromise.then(answer => console.log(answer)); // 42async function asyncExample() {
const a = await new Promise(resolve => resolve(42));
const b = await 42;
let c;
try {
c = await new Promise((_, reject) => reject('Error on await'));
} catch (e) {
console.log(e);
}
console.log(`a: ${a}, b: ${b}, c: ${c}`);
}
asyncExample(); // Error on await
// a: 42, b: 42, c: undefinedLast updated