Eth*_*alm 2 async-await elasticsearch ecmascript-6 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(),
}));
}
Run Code Online (Sandbox Code Playgroud)
问题是我不知道如何让产量等待响应被解决。还有其他方法可以在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 } } },
],
});
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1556 次 |
| 最近记录: |