N
N
n1ksON2020-12-27 23:09:29
React
n1ksON, 2020-12-27 23:09:29

Is redux-saga compiled correctly?

saga.js:

import { call, put, takeLatest } from 'redux-saga/effects'

function* fetchUser(action) {
  try {
    const query = yield call(async () => { 
/* 
больше всего интересует именно call, изначально было написано так:

const query = yield call(() => {
  return fetch('https://api.themoviedb.org/3/movie/top_rated?api_key=<<api_key>>&language=ru&page=1')
  .then(response => response.json())
  .then(result => result);
});

потом VSCode предложил сделать автозамену, и получилось то что ниже. 
Мне кажется это бред, ведь сага и так асинхронно выполняется, верно?
*/
      const response = await fetch('https://api.themoviedb.org/3/movie/top_rated?api_key=<<api_key>>&language=ru&page=1');
      const result = await response.json();
      return result;
    });
    yield put({ type: "USER_FETCH_SUCCEEDED", result: query });
  } catch (e) {
    yield put({ type: "USER_FETCH_FAILED", message: e.message });
  }
}

function* saga() {
  yield takeLatest("USER_FETCH_REQUESTED", fetchUser);
}

export default saga;

Question in code. Was the saga written correctly or did it become correct? Or are both options wrong?
Do not swear, the first time I write a saga, I just tried to read the doc.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
abberati, 2020-12-28
@n1ksON

These pieces of code are absolutely equivalent. That's the only reason the editor was able to suggest a replacement.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question