为什么我的迭代器再次升级?

Joe*_*erg 6 generator node.js promise stripe-payments ecmascript-6

我有以下程序 - 我使用genny.js来处理异步流控制 - 我尝试了与suspend.js相同 - 类似的错误.

我正在使用Stripe nodejs API.

我的迭代器函数似乎被调用了两次 - 这导致了一个错误 - 我不明白为什么它会被调用两次.它一定是一个我没有看到的简单的头脑技巧.

var genny = require('genny')
genny.longStackSupport = true

var stripe = require("stripe")("sk_live_....")

fetchCharges = genny.fn(function* (d) {
  console.log("Before fetchCharges")
  var charges = yield fetchList(d())
  console.log("After fetchCharges - found ", charges.length)
  return true
})

fetchList = genny.fn(function* (done) {
  console.log("before fetchList")
  var results = yield stripe.charges.list({}, done())
  console.log("after fetchList")
  return results.data
})

genny.run(function* (resume) {
  console.log('before run')
  yield fetchCharges(resume())
  console.log('after run')
})
Run Code Online (Sandbox Code Playgroud)

控制台输出是:

> node --harmony genny.js

before run
Before fetchCharges
before fetchList
after fetchList
After fetchCharges - found  10
after run
/Volumes/dev/ingest/node_modules/genny/index.js:50
        else throw e; 
                   ^
Error: callback already called
    at resume (/Volumes/dev/ingest/node_modules/genny/index.js:154:39)
    at throwAt (/Volumes/dev/ingest/node_modules/genny/index.js:49:30)
    at resume (/Volumes/dev/ingest/node_modules/genny/index.js:153:28)
    at tryProcessPending (/Volumes/dev/ingest/node_modules/genny/index.js:41:28)
    at resume (/Volumes/dev/ingest/node_modules/genny/index.js:164:17)
    at null._onTimeout (/Volumes/dev/ingest/node_modules/stripe/lib/StripeResource.js:87:34)
    at Timer.listOnTimeout (timers.js:110:15)
From generator:
    at /Volumes/dev/ingest/genny.js:22:26
Run Code Online (Sandbox Code Playgroud)

现在,如果我用以下函数替换fetchList,它可以正常工作:

fetchList = genny.fn(function* (done) {
  console.log('before doTimeout')
  console.log('1sec break ...')
  yield setTimeout(done(), 1000);
  console.log('after doTimeout')
  return []
})
Run Code Online (Sandbox Code Playgroud)

控制台输出是:

> node --harmony genny.js

before run
Before fetchCharges
before doTimeout
1sec break ...
after doTimeout
After fetchCharges - found  0
after run
Run Code Online (Sandbox Code Playgroud)

为了进一步说明迭代器的next()方法被调用两次这一事实 - 我有另一个(非工作)版本的程序.

var genny = require('genny')
genny.longStackSupport = true

var stripe = require("stripe")("sk_live_...")

fetchCharges = genny.fn(function* (d) {
  console.log("Before fetchCharges")
  var charges = yield fetchList(function(err, cb) {
    console.log("callback")
  })
  console.log("After fetchCharges - found ", charges.length)
  return true
})

fetchList = genny.fn(function* (done) {
  console.log("before fetchList")
  var results = yield stripe.charges.list({}, done())
  console.log("after fetchList")
  return results.data
})

genny.run(function* (resume) {
  console.log('before run')
  yield fetchCharges(resume())
  console.log('after run')
})
Run Code Online (Sandbox Code Playgroud)

它的控制台输出在这里:

> node --harmony genny.js

before run
Before fetchCharges
before fetchList
after fetchList
callback
callback
Run Code Online (Sandbox Code Playgroud)

这很奇怪 - 我不明白.有人比我聪明,请解释一下.

UPDATE

我已经更改了代码来调用条带方法而没有回调或迭代器恢复功能.现在它有效.但是 - 奇怪的是 - 在"结果"处查看控制台.我不明白为什么.所以现在它没有"第二次"调用fetchList迭代器的next()函数 - 但是我看不到它甚至被调用一次!?

var results = yield stripe.charges.list()
Run Code Online (Sandbox Code Playgroud)

这是更新的完整程序.

var genny = require('genny')
genny.longStackSupport = true

var stripe = require("stripe")("sk_live_i6TrEk5lSRM1CmbSZZPsQzKc")

fetchCharges = genny.fn(function* (d) {
  console.log("  fetchCharges {")
  var charges = yield fetchList(d())
  console.log("  } fetchCharges - found ", charges.length)
  return true
})

fetchList = genny.fn(function* (done) {
  console.log("    fetchList {")
  var results = yield stripe.charges.list({}, function(err, results) {
    console.log("results ")
  })
  console.log("    } fetchList")
  return results.data
})

genny.run(function* (resume) {
  console.log('Before run {')
  yield fetchCharges(resume())
  console.log('} after run')
})
Run Code Online (Sandbox Code Playgroud)

这回来了

> node --harmony genny.js

Before run {
  fetchCharges {
    fetchList {
    } fetchList
  } fetchCharges - found  10
} after run
results 
Run Code Online (Sandbox Code Playgroud)

Ber*_*rgi 2

您遇到的问题源于两种异步方法的混合。

stripe API 文档提到

每个资源方法都接受一个可选的回调作为最后一个参数。
此外,每个资源方法都会返回一个承诺。

然而,gennysuspend都会

与 Node 回调约定承诺无缝协作

这就是你的错误:在行中

var results = yield stripe.charges.list({}, done())
Run Code Online (Sandbox Code Playgroud)

你确实同时隐式地使用了两者。done()确实创建了一个传递给 stripe 的回调,但该调用还产生了一个被生成的 Promise,并且 genny/suspend 在其上注册了另一个回调。这导致了Error: callback already called您正在观察的情况。

您可以选择解决问题的方式:

(我推荐后者)