我可以在nodejs(ES7)中开火并忘记承诺吗?
我想用babel运行此代码:
redisClientAsync.delAsync('key');
return await someOtherAsyncFunction();
在没有的异步函数内,等待第一行。可以吗?
我还能如何运行我不在乎的东西?
我可以在没有回调的情况下触发非弹药函数del('键',null)吗?
I would like to run this code with babel:
redisClientAsync.delAsync('key');
return await someOtherAsyncFunction();
inside an async function without await the first line. is this OK?
how else can I run something that I don't care?
Can I just fire the non-promisified function del('key',null) without a callback?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
是的,您可以做到这一点,它将并行运行两个异步功能。您刚刚创造了一个诺言并将其抛弃。
但是,这意味着当拒绝承诺时,您将不会注意到。您只需获取
Unhandledledeptivection
最终,如果不处理,这将使您的流程崩溃。可能还不好。如果您真的不在乎,您就不会首先运行它。因此,您应该清楚和明确您关心的东西(以及不关心):
如果您只想等待并且不在乎结果值,则可以轻松地丢弃结果:
如果您不在乎例外,可以使用它们忽略它们
,可以扔掉,等待它,对它。
如果您想要结果,则必须等待它。如果您关心异常,但不想真正等待,则可能需要与以下功能并行执行:
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.
However, this means that when the promise is rejected you won't notice. You'll just get an
unhandledRejection
eventually which will crash your process if not handled.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:
If you don't care about exceptions, you can ignore them using
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:
是的,在某些情况下,您想这样做是完全合理的。特别是在您不关心结果的情况下 - 一个例子是一个分析跟踪操作,不应干扰业务关键代码。
在许多方面,但是简单地调用承诺功能有效。在这种情况下,您的
del
没有回调的情况可能会起作用,但是有些功能不会防止不传递回调,因此您可以传递空功能(.del('key',((( )=> {})
)。但是,您确实想确保您知道即使您不想破坏代码的操作 - 因此,请考虑添加
process.on.on(“ unhandledledsection' ,
事件处理程序明确忽略这些特定异常或通过以下方式抑制它们:
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.
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:Or preferably, something like:
不在node.js中
节点不要等待持续未决的承诺。如果其他任务已经完成并且事件循环中没有任何剩余的任务,则即使存在未决的承诺,节点过程也将被终止。
对于以下脚本,如果
一些hotherasyncfunction()
在5秒内得到解决,但是redisClientAsync.delasync('key')
需要10秒钟才能执行,则节点进程将被终止从理论上讲5秒后,在第一行解析之前。Not in Node.js.
Node does not wait for ever-pending Promises. If other tasks are already completed and there is nothing left in the event loop, the Node process will be terminated even though there exists pending promise.
For the following script, if
someOtherAsyncFunction()
get resolved in 5 seconds, butredisClientAsync.delAsync('key')
takes 10 seconds to execute, the Node process will be terminated after 5 seconds in theory, before the first line is resolved.从我到目前为止所做的所有研究中,我认为这样做是可以的,只要您保证您不是
的功能,等待
保证 一种方式如果发生这种情况,请处理自己的错误。例如,try-catch
包装整个功能主体,就像您在以下片段中看到的asyncfunction
一样。该函数是同步或异步投掷的都没关系。它保证您的
mainfunction
无论如何都将完成。那是这里的关键。如果您不保证这一点,则必须冒险:
From all the research I've made so far, I think it's fine to do it, as long as you guarantee that the function you are not
await
ing for guarantees a way to handle its own errors in case that happens. For example, atry-catch
wrapping the whole function body, like you see in the following snippet for theasyncFunction
.It doesn't matter if the function throws synchronously or asynchronously. It guarantees the your
mainFunction
will complete no matter what. That's the key point here.If you don't guarantee that, you have to risks: