Can I fire and forget a promise in nodejs (ES7)?

后端 未结 2 2094
梦谈多话
梦谈多话 2020-11-22 15:35

I would like to run this code with babel:

redisClientAsync.delAsync(\'key\');
return await someOtherAsyncFunction();

inside an async functi

2条回答
  •  再見小時候
    2020-11-22 15:59

    inside an async function without await the first line. is this OK?

    Yes, there are cases where you'd want to do this which are perfectly reasonable. Especially where you don't care about the result - one example is an analytics tracking operation that should not interfere with business critical code.

    how else can I run something that I don't care?

    In many ways, however simply calling the promise function works. Your del without a callback would probably work in this case but some functions don't guard against not passing callbacks, so you can pass an empty function instead (.del('key', () => {})).

    You do want to however make sure that you know about it failing, even if you don't want to disrupt the operation of code - so please consider adding a process.on("unhandledRejection', event handler to explicitly ignore these particular exceptions or suppress them via:

    redisClient.delAsync('key').catch(()=>{});
    

    Or preferably, something like:

    redisClient.delAsync('key').catch(logErr);
    

提交回复
热议问题