Wait for effect in effector

1.6k views Asked by At

Is there more "native" way for effector instead of waitForTask?

import { createEffect } from 'effector'
const runTestTask = createEffect({
    async handler (name) {
        // Simulating long task 
        await new Promise(resolve => setTimeout(resolve, 10000));
        return { name };
    }
});

function waitForTask (cb) {
    if (runTestTask.pending.getState()) {
        const unsub = runTestTask.doneData.watch(() => {
            unsub();
            cb({ ... });
        });
    } else {
        cb({ ... });
    }
}
1

There are 1 answers

2
Yan On

Yes, Effect itself has four fields .done - an event that's emitted as soon as the handler has been resolved .fail - an event that's emitted as soon as the handler has been rejected .finally - an event that's emitted as soon as the handler has been finished

if we are talking about dataflow the simplest cases will be the following:

const $testStore = createStore({})
*here is your effect snippet*
$testStore.on(runTestTask.done, (prevState, payload) => payload)

OR

forward({
from: runTestTask.fail,
to: showFailPopup
})

I wrote an article to resolve common misunderstandings