Down-side on calling async function without await

南楼画角 提交于 2020-01-15 04:42:06

问题


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?


回答1:


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 completion 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.



来源:https://stackoverflow.com/questions/49657914/down-side-on-calling-async-function-without-await

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!