javascript - Down-side on calling async function without await - Stack Overflow

Given the code with asyncasync function wierdFunction(){setTimeout(function(){background process that

Given the code with async

async function wierdFunction(){
    setTimeout(function(){
        //background process that does not return
        //Russian roulette
        if ( Math.random() > 0.99 )
            throw new Error('Bang!');
    }, 3000);
}

I only need to call this function asynchronous, I don't need to know when it finished.

app.post('/wierd-endpoint', function (req,res){
    wierdFunction();
    res.json({status:"Running. Now, go away."});
});

Is there any advise against calling it without await keyword?

Given the code with async

async function wierdFunction(){
    setTimeout(function(){
        //background process that does not return
        //Russian roulette
        if ( Math.random() > 0.99 )
            throw new Error('Bang!');
    }, 3000);
}

I only need to call this function asynchronous, I don't need to know when it finished.

app.post('/wierd-endpoint', function (req,res){
    wierdFunction();
    res.json({status:"Running. Now, go away."});
});

Is there any advise against calling it without await keyword?

Share Improve this question asked Apr 4, 2018 at 18:24 lcssancheslcssanches 1,02413 silver badges35 bronze badges 4
  • 1 i mean... what do you gain by doing so? You aren't taking advantage of any of the things that doing so gives you. Why is it async in the first place? – Kevin B Commented Apr 4, 2018 at 18:27
  • What if that function have to do several database modifications, and the user doesn't need to wait. It's steps, pletion and errors are all logged in the database and the user can view the process accessing other web page. – lcssanches Commented Apr 4, 2018 at 19:09
  • Adding async in that case still doesn't change anything. A promise will be created and returned, then garbage collected after being unused. the function being async wouldn't make it faster or slower or have any impact on the code that uses it. it's entirely pointless. – Kevin B Commented Apr 4, 2018 at 19:10
  • It's not pointless. The calling function will run to pletion without waiting for the weirdFunction call. This could make or break responsiveness if the calling function must plete as an UI related init func in a framework. Maybe you really don't care when wierdFunction pletes because it loads data and updates DOM and nothing else depends on it. You should add a catch on the task to avoid unhandled rejections though. See Bob Kerns answer. – AnorZaken Commented Aug 10, 2021 at 13:17
Add a ment  | 

1 Answer 1

Reset to default 7

Just remove the async from wierdFunction(). If you're not using the returned promise and not using await inside, then there's no reason to have it. It just creates an extra promise object for the return that then gets garbage collected so it creates extra overhead and it implies to the caller that the caller can use that promise for something useful.

Is there any advise against calling it without await keyword?

If your operation is truly "fire and forget" and you don't care about pletion or reported errors, then it does not need to return a promise and you can just make it a normal function that initiates an asynchronous operation, but doesn't report any results.

But, you do have to make absolutely sure that a rejected promise never goes unhandled because that may cause nodejs to shutdown. So, even a fire and forget scenario needs to make sure that any errors that can occur are handled somewhere.

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

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

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

关注微信