javascript - 渐进(或迭代)回调

标签 javascript node.js callback

前几天我读到了一种特殊类型的回调,当你重复调用它时,它会随着你的进展而进行。就像第一次调用它时执行 A 并返回一样,然后在下次调用它时执行 B。最终它以某种方式发出信号,表明最终行动已经采取。

我不记得它叫什么,也无法在我的历史记录中找到它,所以我不知道要搜索什么。我需要帮助。

最佳答案

我认为您可能正在寻找生成器函数。 ES6 引入了它。

Calling a generator function does not execute its body immediately; an iterator object for the function is returned instead. When the iterator's next() method is called, the generator function's body is executed until the first yield expression, which specifies the value to be returned from the iterator

参见MDN documentation .

示例:

function* idMaker() {
  var index = 0;
  while (index < 3)
    yield index++;
}

var gen = idMaker();

console.log(gen.next().value); // 0
console.log(gen.next().value); // 1
console.log(gen.next().value); // 2
console.log(gen.next().value); // undefined

关于javascript - 渐进(或迭代)回调,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42858520/

相关文章:

javascript - 如何使用 bootstrap-select 在每个选择选项上添加工具提示

php - 使用 php 和 javascript/JQuery 按类或 id 检索图像源

node.js - 将图像转换为多行灰度像素值

javascript - 未捕获的类型错误 : Illegal invocation during a post request

javascript - JS 中的对象字面量符号差异

javascript - 使用计数预分配记录

javascript - 如何在 Angular JS 中为 Morris JS 图创建指令

c# - Blazor 事件回调问题父/子组件 "does not have a property matching the name ' OnClickCallback'"

clojure - 当 Clojure promise 实现或失败时,如何听取它?

javascript - React - 处理长链的嵌套回调