在异步之外使用 await [英] Use await outside async
问题描述
有没有办法让 javascript await
关键字在 async
函数之外工作?我希望能够冻结整个调用堆栈(而不仅仅是 async
函数的其余部分),一旦特定的承诺返回一个值就可以恢复.可悲的是,像这样强大的 await
目前已被屏蔽或尚未实现.我试图让 nodent.js 工作,但由于我的自定义加载器和动态函数,不幸的是不切实际.
Is there a way to make the javascript await
keyword work outside async
functions? I would like to be able to freeze the entire call-stack (instead of just the rest of the async
function), to be resumed once the particular promise returns a value. Sadly a powerful await
like that is currently gimped or not implemented yet. I tried to make nodent.js work, but due to my custom loader and dynamic functions it's unfortunately impractical.
推荐答案
鉴于您正在寻找 hack,而不是适当的基于 Promise 的并发解决方案,请查看 node-fibers(有类似的,但这是最流行的,围绕它构建了多个抽象).它确实允许您在任何同步函数中暂停当前光纤,直到发生在不同光纤中运行的异步事件.这当然是一个可怕的想法,但好吧……
Given you are looking for a hack, not a proper promise-based concurrency solution, have a look at node-fibers (there are similar ones, but afaik this is the most popular, with multiple abstractions built around it). It does allow you to halt the current fiber in any synchronous function until something asynchronous happens that runs in a different fiber. Which of course is a horrible idea, but well…
这篇关于在异步之外使用 await的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!