ferrybig icon

Redux saga nodejs

ferrybig | PRO | 09/04/20 03:13:05 PM UTC | 0 ⭐ | 1795 👁️ | Never ⏰ | []
JavaScript |

1.42 KB

|

None

|

0 👍

/

0 👎

const { call, put, delay, all } = require('redux-saga/effects');
const { runSaga, stdChannel }  = require("redux-saga");
 
// Setup
const channel = stdChannel/*<never>*/();
const myIO/*: RunSagaOptions<never, never>*/ = {
    // this will be used to orchestrate take and put Effects
    channel,
    // this will be used to resolve put Effects
    dispatch(output) {
        throw new Error('[PUT] This saga is not setup with a store connection: ' + output);
    },
    // this will be used to resolve select Effects
    getState() {
        throw new Error('[SELECT] This saga is not setup with a store connection');
    },
};
 
// Main code
function callApi(url) {
    console.log(new Date() + 'Calling ' + url);
    return Promise.resolve(['Yass']);
}
 
function* callApiWithDelay(url) {
    const result = yield call(callApi, url);
    yield delay(5000);
    return result;
}
 
function* fetchResults() {
    console.log(new Date() + 'fetchResults starting');
    const listing = yield callApiWithDelay('/list')
    for(const item of listing) {
        yield callApiWithDelay('/res/'+item)
    }
    console.log(new Date() + 'fetchResults finished');
}
function* mainLoop() {
    yield delay(100); // Startup delay so console logs make sense
    while(true) {
        yield all([
            call(fetchResults),
            //delay(1000 * 60 * 20),
            delay(1000 * 7),
        ]);
    }
}
 
const task = runSaga( myIO, mainLoop )
 

Comments