0

My saga.ts :

export function* postSomething(object:object){  
        yield postRequest(object);
        const Objects=yield getRequest();
        console.log(Objects); 
        yield put(showObject(Objects));
}

**db.json is my little database

My db.json is empty at the beginning, and after first postRequest(object), db.json will have one object,but when I try to get that object with getRequest I get [ ] (empty array)!?Why?

My console.log(Object) doesn't print anything.

Next time when I do postRequest(object) and post to my db.json one more object, that means db.json will have two objects, but after that, when I try to pull objects with getRequest(), my console.log print only one object?Why?

How to get all objects from db.json after postRequest() ?

postRequest(object)(post object to db.json using fetch,method "post") is service function which communicate with db.json,

getRequest()( fetch(url) which need to return all object from db.json ) .

2 Answers2

0

This is a generator function so every time it returns it restarts after the last yield statement so the second time through, it starts at line 3.

Redux saga has the call effect:

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

export function* postSomething(object:object){  
        yield call(postRequest, object);
        const Objects=yield call(getRequest());
        console.log(Objects); 
        yield put(showObject(Objects));
}

In your case, though, it seems like you should probably be using 2 chained sagas here.

Dov Rine
  • 810
  • 6
  • 12
0

You should be using the call effect from redux-saga API:

export function* postSomething(object:object){  
        yield call(postRequest(object));
        const Objects = yield call(getRequest());
        console.log(Objects); 
        yield put(showObject(Objects));
}

In general, it would be better pattern using 2 different sagas for handling each request. that way you can handle success/failure with redux in much more ease and clarity.

Tomer
  • 1,521
  • 1
  • 15
  • 26