我可以发射和忘记的NodeJS(ES7)的承诺? [英] Can I fire and forget a promise in nodejs (ES7)?

查看:458
本文介绍了我可以发射和忘记的NodeJS(ES7)的承诺?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想运行此code。与巴贝尔:

I would like to run this code with babel:

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

没有的await 的第一行异步函数中。这是OK?

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

怎么我还能跑的东西,我不关心吗?

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

我可以只火而回调非promisified功能德尔('键',NULL)?

Can I just fire the non-promisified function del('key',null) without a callback?

推荐答案

是的,你能做到这一点,这将并行运行两个异步功能。您刚刚创建了一个承诺而丢弃。

Yes, you can do that, and it will run the two asynchronous functions in parallel. You've just created a promise and thrown it away.

然而,这意味着,当承诺被拒绝,你不会注意到。你只需获得 unhandledRejection 最终的。

However, this means that when the promise is rejected you won't notice. You'll just get an unhandledRejection eventually.

这样行吗?我如何运行的东西,我不关心吗?

Is this OK? How can I run something that I don't care?

也许它并不确定。如果你真的不会在意,你没有摆在首位运行它。所以,你应该清楚和明确的,你关心什么(什么不可以):

Probably it's not OK. If you truly wouldn't care, you hadn't run it in the first place. So you should be clear and explicit what you care about (and what not):


  • 请你想等? (副作用)

  • 请您需要的结果?

  • 你想捕捉异常?

如果你只是想等待,不在乎结果值,你可以很容易地扔掉的结果:

If you only want to wait and don't care for the result value, you can easily throw away the result:

void (await someAsyncFunction()); // or omit the void keyword,
                                  // doesn't make a difference in an expression statement

如果你不关心异常,你可以用忽略它们。

If you don't care about exceptions, you can ignore them using

… someAsyncFunction().catch(function ignore() {}) …

您可以抛出了,等待着它,用它做任何事情。

You can throw that away, await it, do anything with it.

如果您想要的结果,你必须等待它。如果你关心异常,但真的不希望等待,可能要平行于以下功能执行:

If you want the result, you have to await it. If you care about exceptions, but don't really want to wait, you may want to execute it in parallel with the following functions:

var [_, res] = await Promise.all([
    someAsyncFunction(), // result is ignored, exceptions aren't
    someOtherAsyncFunction()
]);
return res;

这篇关于我可以发射和忘记的NodeJS(ES7)的承诺?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆