3 ms·
In JavaScript, "await" is syntactic sugar for promises. async function myFunction() { a(); await b(); let d = c(); return d; } becomes som
by jdlshore 5y ago
In JavaScript, "await" is syntactic sugar for promises.
async function myFunction() {
a();
await b();
let d = c();
return d;
}
becomes something like this:
function myFunction() {
return new Promise((resolve, reject) => {
a();
b().then(() => {
try {
let d = c();
resolve(d);
}
catch (err) {
reject(err);
}
}).catch(reject);
});
}
If you didn't mark `myFunction()` as async, it wouldn't set up the `new Promise()` scaffolding, and there would be no `resolve()` or `reject()` to call.
Note that you can call an async function from a non-async function. Assuming b() is async and a() and c() are not, a manual approach looks like this:
function myFunction(callback)
a();
b().then(() => {
try {
let d = c();
return callback(null, d);
}
catch (err) {
return callback(err);
}
}).catch(callback);
}