elasticsearch - 如何在 redux saga 中使用 async await?

标签 elasticsearch ecmascript-6 async-await redux-saga

我正在尝试使用 ES javascript api 从 Elastic Search 获取数据并将其显示在我的 React Redux Redux-Saga 代码中。

function* getData() {
  const response = async () => await client.msearch({
     body: [
    // match all query, on all indices and types
    {},
    { query: { match_all: {} } },

    // query_string query, on index/mytype
    { index: 'myindex', type: 'mytype' },
    { query: { query_string: { query: '"Test 1"' } } },
  ],
  });

  yield put(Success({
    Data: response(),
  }));
}

问题是我不知道如何让产量等待响应得到解决。
还有其他方法可以在 redux saga 和 es javascript-client 中使用 promise 吗?

最佳答案

我想到了。将答案放在这里供任何寻找它的人使用。

function* getData(data) {
  const response = yield call(fetchSummary, data);
  yield put(Success({
    Data: response,
  }));
}

async function fetchSummary(data) {
  return await client.msearch({
     body: [
     // match all query, on all indices and types
     {},
     { query: { match_all: {} } },

     // query_string query, on index/mytype
     { index: 'myindex', type: 'mytype' },
     { query: { query_string: { query: data.query } } },
    ],
  });
}

关于elasticsearch - 如何在 redux saga 中使用 async await?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51951839/

相关文章:

date - Elasticsearch日期直方图:从开始和结束日期计算直方图 'field'?

elasticsearch - Kibana-@message字段中的唯一单词的积点计数

python - python-如何将C函数实现为可等待的(协程)

wpf - 异步等待不等待

c# - 等待取消任务的简洁方法?

elasticsearch - 如何在 elasticsearch 中只存储有限数量的文档。

json - 使用Elastic Search搜索多个词

javascript - 从对象导入属性

javascript - Brain.js 倒数/反转训练(根据输出预测输入)

javascript - 有人可以解释 forEach 循环吗?