0

I am attempting to use fetch as follows

export function* loadData() {
  console.log("Saga: Loading Data...");
  try {
    const response = yield call(request, requestURL,
    { method: 'GET', credentials: 'include', headers: {
    'Content-Type': 'application/json', }  });
    // Nothing past this point is ever touched
    if (response.success) {
     console.log("SUCCESS!!!!");
     ...

export default function request(url, options) {
  return fetch(url, options)
    .then(checkStatus)
    .then(parseJSON);
}

CheckStatus confirms a successful status on the data

function parseJSON(response) {
  if (response.status === 204 || response.status === 205) {
    return null;
  }
  var responseClone = response.clone();
  console.log(responseClone.json());
  return response.json();
}

Not enough rep to post a screenshot of the clone output, but essentially chrome dev outputs

SAGA: Loading Data...
[[PromiseStatus]]:"resolved"
[[PromiseValue]]:Object

And the object contains all the data I need, the code just wont continue past that response call.

  • 1
    Can you explain to me why you are using a Generator (`function*`)? https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/function* – Cody G Apr 19 '18 at 20:17
  • Fetch is asynchronous, you can't return its result normally. – Barmar Apr 19 '18 at 20:19
  • you could add a link to the output's screenshot , did you `console.log()` in the `catch` block ? , ( no `loadData.next()` ? ) – Taki Apr 19 '18 at 20:25
  • 1
    @CodyG. redux-saga works like that – Bergi Apr 19 '18 at 20:29
  • I understand fetch is asynchronous, and I believe I am using my then statements correctly. Not sure why this was marked duplicate. – Manuel Escalante Apr 19 '18 at 20:33
  • Ah, @Bergi I see where this style might be coming from now, thanks. – Cody G Apr 19 '18 at 20:34
  • Yeah, if context helps, I am using react-boilerplate. Redux saga calls this function and after the success I dispatch my load data action. – Manuel Escalante Apr 19 '18 at 20:36
  • Well, loadData won't run anything after the yield statement unless the iterator is called until complete=true, so whatever is using loadData if it's only calling .next() once then it won't run. But I would have imagined this would have been more automatic for you in your library. – Cody G Apr 19 '18 at 20:49
  • @ManuelEscalante I think what you didn't understand is that `response.json()` (or `responseClone.json()`) is asynchronous as well, that's why I closed as a dupe. However, your log seems to suggest that this inner promise resolves just fine, so I cannot see reason why redux-saga wouldn't continue – Bergi Apr 19 '18 at 20:49
  • I'm certainly not an expert on this, but if it is resolving, and you cannot see why saga isn't continuing, why are you still marking my questions as duplicate? I appreciate link to it though. – Manuel Escalante Apr 19 '18 at 20:59
  • @Bergi Could you remove the duplicate? – Manuel Escalante Apr 19 '18 at 21:59
  • @Bergi Can you please remove the duplicate. – Manuel Escalante Apr 20 '18 at 20:06
  • Can you simplify the example by returning a `Promise.resolve()` instead of doing the complicated `fetch`, and still reproduce the issue? You will also need to show us how `loadData` is called, if your question is why it doesn't continue. – Bergi Apr 20 '18 at 21:41

0 Answers0