SHARE
    TWEET
    ferrybig

    Redux saga nodejs

    Sep 4th, 2020
    1,658
    0
    Never
    Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
    1. const { call, put, delay, all } = require('redux-saga/effects');
    2. const { runSaga, stdChannel } = require("redux-saga");
    3. // Setup
    4. const channel = stdChannel/*<never>*/();
    5. const myIO/*: RunSagaOptions<never, never>*/ = {
    6. // this will be used to orchestrate take and put Effects
    7. channel,
    8. // this will be used to resolve put Effects
    9. dispatch(output) {
    10. throw new Error('[PUT] This saga is not setup with a store connection: ' + output);
    11. },
    12. // this will be used to resolve select Effects
    13. getState() {
    14. throw new Error('[SELECT] This saga is not setup with a store connection');
    15. },
    16. };
    17. // Main code
    18. function callApi(url) {
    19. console.log(new Date() + 'Calling ' + url);
    20. return Promise.resolve(['Yass']);
    21. }
    22. function* callApiWithDelay(url) {
    23. const result = yield call(callApi, url);
    24. yield delay(5000);
    25. return result;
    26. }
    27. function* fetchResults() {
    28. console.log(new Date() + 'fetchResults starting');
    29. const listing = yield callApiWithDelay('/list')
    30. for(const item of listing) {
    31. yield callApiWithDelay('/res/'+item)
    32. }
    33. console.log(new Date() + 'fetchResults finished');
    34. }
    35. function* mainLoop() {
    36. yield delay(100); // Startup delay so console logs make sense
    37. while(true) {
    38. yield all([
    39. call(fetchResults),
    40. //delay(1000 * 60 * 20),
    41. delay(1000 * 7),
    42. ]);
    43. }
    44. }
    45. const task = runSaga( myIO, mainLoop )
    Advertisement
    Add Comment
    Please, Sign In to add comment
    Public Pastes
    We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
    Not a member of Pastebin yet?
    Sign Up, it unlocks many cool features!

    AltStyle によって変換されたページ (->オリジナル) /