question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

yield from callback function/event listener?

See original GitHub issue

Is there a way to yield from callback function. e.g.

function* mySaga() {
    hashHistory.listenBefore(location => {
        yield put({ type: 'LOCATION_CHANGE', payload: location });
        var state = yield select();
        return true; // or false depending on state
        // or do some more yield stuff        
    });
}

How do you handle this kind of side effects?

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:3
  • Comments:12 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
evgenyt1commented, Apr 24, 2016

@yelouafi I spent few days trying and debugging a bunch of ways, including generating and handling promises, using callbacks, etc. However all of this complicated my code much and was unreliable (as I want different sagas handle different routes and depend on each other result).

My workaround is not to use sagas in this case. Just a special list or routing handlers, that are called synchronously in predictable order by some general entry point (events of router), get state/dispatch and routing stuff as parameters, with final “fallback” handler that just approves user navigation (it wasn’t handled by routing handlers).

In my case I have a routing directory in src with handleAuth.js, handlePage1.js, handlePage2.js etc. So the order is also important, and event handlers can depend on other handler result. As auth goes first, than page handlers that can fetch some data from store, redirect (using dispatch), etc etc

1reaction
zalmoxisuscommented, Apr 8, 2016

Hey @evgenyt1.

One solution would be to use channels as described on SO. It is also planned to be supported out of the box.

Another solution would be just to use dispatch (and getState in your case) parameters passed into rootSaga. Though it seems uglier than above one, I usually use this solution for binding events. A bit harder to test, but cleaner and guaranteed that you’ll not miss any event.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Why can't the generators next function be a callback function ...
The way that event listen works is that it will call the function handle using call and assign the this binding from its...
Read more >
Cleaning up callbacks with yield - Aaron Powell
In my last post we took a journey on how to make a function execute in a delayed fashion by using the new...
Read more >
Developers - yield from callback function/event listener? -
Is there a way to yield from callback function. e.g. function* mySaga() { hashHistory.listenBefore(location => { yield put({ type: 'LOCATION_CHANGE', ...
Read more >
A Study on Solving Callbacks with JavaScript Generators
Generator functions can suspend execution with the yield keyword, and pass values back and forth when resuming and suspending. This means that ...
Read more >
Creating streams in Dart
Avoid producing events when the listener has requested a pause. An async* function automatically pauses at a yield statement while the stream subscription ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found