试图了解node.js中的生成器/yield-什么执行异步函数? [英] Trying to understand generators / yield in node.js - what executes the asynchronous function?

查看:82
本文介绍了试图了解node.js中的生成器/yield-什么执行异步函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Node.js现在具有生成器.

Node.js now has generators.

我的理解是,生成器可用于编写看起来更加线性的代码,并避免回调地狱和末日风格编码的金字塔.

My understanding is that generators can be used to write code that appears to be much more linear and avoids callback hell and pyramid of doom style coding.

因此,到目前为止,我的理解是,在生成器内部,代码将执行直到达到"yield"语句为止.此时,生成器功能的执行将中止. yield语句指定一个返回值,该值可以是一个函数.通常,这将是一个阻塞的I/O功能-通常需要异步执行的功能.

So to this point, my understanding is that inside a generator, code executes until it reaches a "yield" statement. Execution of the generator function suspends at this point. The yield statement specifies a return value which may be a function. Typically this would be a blocking I/O function - one that would normally need to be executed asynchronously.

yield的return函数返回到称为generator的任何东西.

The yield's return function is returned to whatever called the generator.

我的问题是,此时会发生什么?究竟是什么执行收益率返回的阻塞I/O功能?

My question is, what happens at this point? What exactly executes the blocking I/O function that the yield returned?

编写似乎线性的生成器/yield代码是否正确,是否需要某种特定类型的函数来调用生成器,该函数循环遍历生成器并执行yield所返回的每个asynch函数并将asynch函数的结果返回到生成器中?

Is it correct that to write generator/yield code that appears to be linear, there needs to be a specific sort of function that is calling the generator, a function that loops through the generator and executes each asynch function returned by the yield and returns the result of the asynch function back into the generator?

我仍然不清楚确切地如何执行yield所返回的asynch函数.如果它是由调用生成器的函数执行的,那么它是否异步执行?我猜是这样,因为否则将导致行为阻塞.

It's still not clear to me exactly how the asynch function returned by the yield is executed. If it is executed by the function that calls the generator, is it executed asynchronously? I'm guessing so because to do otherwise would result in blocking behaviour.

总结我的问题:

  1. 要使用生成器编写线性"异步代码,是否有必要在生成器上进行迭代的调用函数,将产生的函数作为回调执行,并将回调结果返回到生成器中?
  2. 如果对问题1的回答为是",那么究竟如何以异步方式执行所产生的函数?

任何人都可以提供关于整个过程如何更好的概述/摘要吗?

Can anyone offer a better overview/summary of how the whole process works?

推荐答案

使用生成器编写异步代码时,您要处理两种类型的函数:

When writing async code with generators you are dealing with two types of functions:

    function声明的
  • normal 函数.这些函数无法产生.您无法以同步方式编写异步代码,因为它们可以完成.您只能通过回调处理异步完成(除非您调用node-fibers库或代码转换之类的额外功能).
  • function*声明的
  • generator 函数.这些函数可以产生.您可以在它们内部以同步样式编写异步代码,因为它们可以让步.但是您需要一个伴随函数来创建生成器,处理回调并在每次触发回调时通过next调用恢复 generator .
  • normal functions declared with function. These functions cannot yield. You cannot write async code in sync style with them because they run to completion; you can only handle asynchronous completion through callbacks (unless you invoke extra power like the node-fibers library or a code transform).
  • generator functions declared with function*. These functions can yield. You can write async code in sync style inside them because they can yield. But you need a companion function that creates the generator, handles the callbacks and resumes the generator with a next call every time a callback fires.

有几个实现伴随功能的库.在大多数这些库中,伴随函数一次只能处理一个function*,您必须在代码中的每个function*周围放置一个包装器.星系库(我写的)有点特殊,因为它可以处理function*而不用中间包装来调用其他function*.伴随函数有点棘手,因为它必须处理一堆生成器.

There are several libraries that implement companion functions. In most of these libraries, the companion function handles a single function* at a time and you have to put a wrapper around every function* in your code. The galaxy library (that I wrote) is a bit special because it can handle function* calling other function* without intermediate wrappers. The companion function is a bit tricky because it has to deal with a stack of generators.

由于您的function*与伴随函数之间的yield/next跳动很小,执行流程可能很难理解.理解流程的一种方法是使用您选择的库编写示例,在代码和库中添加console.log语句,然后运行它.

The execution flow can be difficult to understand because of the little yield/next dance between your function* and the companion function. One way to understand the flow is to write an example with the library of your choice, add console.log statements both in your code and in the library, and run it.

这篇关于试图了解node.js中的生成器/yield-什么执行异步函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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