Join egghead, unlock knowledge.

Want more egghead?

This lesson is for members. Join us? Get access to all 3,000+ tutorials + a community with expert developers around the world.

Unlock This Lesson
Become a member
to unlock all features

Level Up!

Access all courses & lessons on egghead today and lock-in your price for life.


    Implement Asynchronous Side Effects with Non-Blocking Saga Effects

    3 - 4

    When we need to run side effects and do not care about a return value, we should use non-blocking effects. This way we can iterate through our sagas as efficiently as possible. Some popular non-blocking effects are put and fork.



    Become a Member to view code

    You must be a Pro Member to view code

    Access all courses and lessons, track your progress, gain confidence and expertise.

    Become a Member
    and unlock code for this lesson
    orLog In




    Instructor: Let's import select, throttle, fork, spawn, and cancel. These five effects, as well as put, are considered non-blocking effects. This means that they're used inside of our saga generators.

    The generator will continue to iterate through without waiting for a resolved value. On the other hand, call is blocking because we need to wait for a return result to use inside of our put. Let's do const selector = yield select and a function.

    Here, we're using the select effect, which will instruct the middleware to invoke the provided selector on the current store state. Ideally, we would create the selector method elsewhere and import it at the top of this file.

    We can see this in action by console logging the selector. Now, if we open up our UI and click on our "Load more" button, you can see that we get there our state at the time before our call is made and our put is dispatched.

    If we move this down to the bottom and now try it, you can see that now we get our reducer with our results. Let's remove our select effect. We'll paste in two new generator functions.

    Sometimes, we need to throttle actions that are dispatched. A common example is when a user is typing text into an input box. Throttle has three parameters. The first is a time in milliseconds.

    The second is a pattern or the type of a dispatch action creator. The third is a generator function. Here, we're basically saying once input change is seen, we will not spawn a new handle input saga until this amount of time is satisfied.

    Now, let's remove these generators. Back inside fetch person, we do yield fork API and a URL. Fork does the same thing as call. The first parameter is a function. The rest of fork's parameters are spread out into this first function parameter.

    However, the difference is fork will call the API with this URL and then continue on through the saga without stopping or blocking the rest of the effects. This is handy when you need to send a request but don't need the return data.

    Even though fork is non-blocking, the fetch person saga will not return until all fork tasks come to a resolution. Spawn is similar to fork. However, it creates a detach task.

    These remain independent from its parent and act like top-level tasks. Fetch person will not wait for the spawn task to terminate before returning. Also, all events which may affect the parent or the spawn are completely independent.

    Let's do const dogs. We'll do yield take canceled as well as yield cancel dogs. We'll paste in this canceled action creator. Once fork tasks are initiated, we may need to cancel them.

    By using this take, we are saying after we've called in put, if we see this cancel action creator, we need to cancel the in-flight request if it's still pending. Cancel will stop the effect it's provided wherever it's at.

    This is handy when someone is logging into a site and after clicking "Log in," they instantly click "Logout" or "Cancel" before the response is returned. This can be used in both spawned and forked tasks.