Which is correct way to return nothing when using await?
Usage:
exports.run = async => {
try {
await update(data);
} catch(err) {
console.error(err);
}
};
Function:
function update() {
if (data) {
return updateRecord(data).promise();
}
// does not need to be rejected.
return; //Is this correct?
return Promise.resolve(); //Is this correct?
}
Which is correct way to return nothing when using await?
Usage:
exports.run = async => {
try {
await update(data);
} catch(err) {
console.error(err);
}
};
Function:
function update() {
if (data) {
return updateRecord(data).promise();
}
// does not need to be rejected.
return; //Is this correct?
return Promise.resolve(); //Is this correct?
}
Share
Improve this question
asked Jul 10, 2019 at 16:06
user88432user88432
4171 gold badge6 silver badges14 bronze badges
4
-
return
andreturn Promise.resolve()
will work identically for an async function – Explosion Pills Commented Jul 10, 2019 at 16:08 -
1
It doesn't matter WRT
await
. It can handle both Promises and other values. However if a function returns a Promise it is best to make it always return a Promise (it's a good idea in general to always return the same data type from a function). If you sometimes return a Promise and sometimesundefined
, then someone might call the function and try to use.then
on the result which will throw an error if it isundefined
. – Paul Commented Jul 10, 2019 at 16:10 -
You can either make the function
async
and leave off the return statement or have an empty return statement, or you can usereturn Promise.resolve( );
whether or not you make it async. – Paul Commented Jul 10, 2019 at 16:11 -
In my opinion the nicest option is to leave out the return statement and just make the function
async
. – Paul Commented Jul 10, 2019 at 16:12
3 Answers
Reset to default 3The simplest way is to not use return
statement at all, so that the returned promise resolves with undefined
at the end of the function:
async function update() {
if (data)
return updateRecord(data).promise();
}
But you can spell it out explicitly as well, and when you return a Promise.resolve(…)
then you don't need to mark the function as async
:
async function update() {
if (data)
return updateRecord(data).promise();
return;
}
async function update() {
if (data)
return updateRecord(data).promise();
return undefined;
}
function update() {
if (data)
return updateRecord(data).promise();
return Promise.resolve();
}
function update() {
if (data)
return updateRecord(data).promise();
return Promise.resolve(undefined);
}
async function update() {
return data ? updateRecord(data).promise() : undefined;
}
function update() {
return data ? updateRecord(data).promise() : Promise.resolve();
}
function update() {
return data ? updateRecord(data).promise() : Promise.resolve(undefined);
}
They all achieve the same. Use the simplest and most readable.
Since update()
isn't async
, return Promise.resolve()
for "nothing". That way the signature (in TypeScript syntax for convenience) of the function will be (...) => Promise
, not (...) => Promise | undefined
.
You can also make the function async
, in which case returning undefined
will be wrapped into a promise internally.
You can just leave out any additional explicit returns- just add the async keyword to the function and it will execute properly
async function update() {
if (data) {
return updateRecord(data).promise();
}
}