javascript - Return empty when using await? - Stack Overflow

Which is correct way to return nothing when using await?Usage:exports.run = async => {try {await upd

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 and return 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 sometimes undefined, then someone might call the function and try to use .then on the result which will throw an error if it is undefined. – 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 use return 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
Add a ment  | 

3 Answers 3

Reset to default 3

The 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();
    }
}

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745142025a4613473.html

相关推荐

  • javascript - Return empty when using await? - Stack Overflow

    Which is correct way to return nothing when using await?Usage:exports.run = async => {try {await upd

    5小时前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信