How to use async await inside redux saga?
Asked Answered
M

1

8

I'm trying to use ES javascript api to get data from Elastic Search and show it in my React Redux Redux-Saga code.

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

The problem is I don't know how to make the yield wait for the response to be resolved. Is there any other way to use a promise inside redux saga and es javascript-client ?

Myriagram answered 21/8, 2018 at 15:16 Comment(0)
M
16

I figured it out. Putting the answer here for anyone looking for it.

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 } } },
    ],
  });
}
Myriagram answered 21/8, 2018 at 18:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.