johnnash
johnnash

Reputation: 424

Yield inside .then function of promise response throws error

Following code is throwing error:

  syncData().then((x) => yield put(loginUserSuccess(responseData)));

Error: The keyword 'yield' is reserved.

Sync data handles asynchronous operations of fetching data.I want to use yield put(provided by sagas.js) to trigger the action login user success after sync data has been performed.

Core of syncData function is as follows:

  export default async function syncData(dataType) {
      await Promise.all([syncData1(), syncData2()]);
  }

Upvotes: 5

Views: 3659

Answers (1)

CD..
CD..

Reputation: 74096

Try separating it like this, using the call effect and then the put effect:

try {
  const res = yield call(syncData) 

  yield put(loginUserSuccess(res))

} catch(e) {

  yield put(loginUserFail(e))

}

Upvotes: 7

Related Questions