在 javascript 中将生成器作为回调传递

Passing a generator as callback in javascript

在我的 redux-saga 函数中,我需要在回调中产生一个 put。所以我传递一个生成器作为回调。但是我的回调没有执行。相反,如果我使用匿名函数,则回调运行。

此日志数据:

  Tabletop.init({
    key: action.key,
    callback: googleData => {
      console.log(googleData);
    },
    simpleSheet: true
  });

这不记录数据:

  Tabletop.init({
    key: action.key,
    callback: yield function*(googleData) {
      console.log(googleData);
      yield put(setProblems(googleData));
    },
    simpleSheet: true
  });

我在网上看到你可以在承诺的一部分中屈服。 Tabletop.js 支持这样的承诺:

function init() {
  Tabletop.init( {
    key: 'https://docs.google.com/spreadsheets/d/0AmYzu_s7QHsmdDNZUzRlYldnWTZCLXdrMXlYQzVxSFE/pubhtml',
    simpleSheet: true }
  ).then(function(data, tabletop) { 
    console.log(data)
  })
}

但这种方式对我不起作用,因为我收到此错误:

TypeError: undefined is not a function (near '...}).then(function (data, tab...')

[https://github.com/jsoma/tabletop/issues/175]

有人能告诉我正确的方法吗?获取数据后,我想将其设置在我的减速器中。

这就是我让它工作的方式。如果有任何其他方法可以使这项工作,请告诉我。

我创建了一个返回承诺的函数:

function fetchProblemsPromise(key) {
  return new Promise(resolve => {
    Tabletop.init({
      key: key,
      callback: googleData => {
        resolve(googleData);
      },
      simpleSheet: true
    });
  });
}

然后投降了:

const data = yield call(fetchProblemsPromise, [action.key]);
yield put(setProblems(data));

这帮我弄明白了:
https://github.com/redux-saga/redux-saga/issues/508