【问题标题】:How to use async await inside redux saga?如何在 redux 传奇中使用异步等待?
【发布时间】:2019-01-27 19:53:07
【问题描述】:

我正在尝试使用 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 吗?

【问题讨论】:

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


    【解决方案1】:

    我想通了。在这里为任何寻找答案的人提供答案。

    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 } } },
        ],
      });
    }
    

    【讨论】:

      猜你喜欢
      • 2018-03-26
      • 1970-01-01
      • 2017-06-15
      • 2020-07-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多