3

Spent few hours trying to figure out how to run sagas inside a map in parallel.

Tried out what I found : yield result.map(item => call(api.endpoint, item)) But nothing seems to happen.

Here's what I was doing :

export function* syncCanvasesByFolder(<someParams>) {
    yield call(api.syncCanvasForFolder, <someParam>);
}

export default function* syncAllFoldersForSurvey(<someParams>) {

    // Get list of folder for a survey
    const folders = yield call(data.getFolderBySurvey, surveyId);

    //For each folder sync canvases

    yield all(folders.map(folder => {
           call(syncCanvasesByFolder, folder.id, lastSyncedAt, access_token)  
    }));

/*Sequential call*/
// for (const i in folders) {
//     if (folders.hasOwnProperty(i)) {
//         yield call(syncCanvasesByFolder, folders[i].id, lastSyncedAt, access_token);
//       //  yield call(canvasSyncSaga.syncAllCanvasesForFolder, folders[i].id, lastSyncedAt);
//     }
// }
}

Sequential call when done in a for loop is working. But nothing seems to happen inside the map. Not getting any exception/errors as well.

syncCanvasesByFolder never gets called when I am using map.

Rohit Poudel
  • 1,793
  • 2
  • 20
  • 24
Anil Krishnan
  • 31
  • 1
  • 2
  • Resolved: Had to return inside map `yield all(folders.map(folder => { call(syncCanvasesByFolder, folder.id, lastSyncedAt, access_token) }));` – Anil Krishnan Jul 20 '17 at 00:14

2 Answers2

4

To implicitly return, don't use curly brackets {} inside function.

With Brackets (Doesn't Work)

yield all(folders.map(folder => {
    call(syncCanvasesByFolder, folder.id, lastSyncedAt, access_token)  
}));

Without Brackets (Works)

yield all(folders.map(folder => call(syncCanvasesByFolder, folder.id, lastSyncedAt, access_token)  
));
Sheikh Abdul Wahid
  • 2,623
  • 2
  • 25
  • 24
0

With Brackets you have to use return

yield all(folders.map(folder => {
    return call(syncCanvasesByFolder, folder.id, lastSyncedAt, access_token)  
}));

Without Brackets return is implicit

yield all(folders.map(folder => call(syncCanvasesByFolder, folder.id, lastSyncedAt, access_token)  
));