When using async/await
syntax we write it like so:
async function getAllRecords() {
let records = await getDataFromServer();
// Do something with records
}
In other words, if you want to await
within a function, you need to make sure you use the async
keyword as well (as that changes how this code is handled behind the scenes). All good so far.
My question is: are there cases where you define a function using the async
keyword, without also using await
within the body of the function?
For instance, I came across this:
async sybaseUpdateCustomer(exId, customer, cardType, lastFour, expireMonth, expireYear, billingStreet, billingZip, defaultPaymentMethod) {
return new Promise((resolve, reject) => {
console.log(`Updating customer for customer ${customerId} in sybase.`);
sybase.query(
"INSERT INTO cc_customers (ex_id, identifier, card_type, last_four, expiration_month, expiration_year, address, zip, isDefault) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)",
[exId, customerId, cardType, lastFour, expireMonth, expireYear, billingStreet, billingZip, defaultPaymentMethod],
(res) => {
if (res.status == 'failure') return reject(res.data);
return resolve(true);
}
);
});
}
Notice the async
keyword is being used, but there is no await
anywhere in the body of the function. Is this correct syntax? And, if so, what is async
doing here, and is it necessary for the function to execute correctly?