如何在 redux saga 中使用异步等待?

How to use async await inside 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(),
  }));
}

问题是我不知道如何让 yield 等待响应被解决。 还有其他方法可以在 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 } } },
    ],
  });
}