Reputation: 85785
I am using mobx state tree and mobx for UI Stuff.
Now when I save something to db, after the request is done I want to update the ui(ie my mobx state).
I need to know when the flow is finished.
myFlow: flow(function* () {
// do stuff here.
}),
now I see that a promise is returned, so I thought of just doing
myFlow.then()
which works but I am wondering if this is the property way or if there is another way to do this(async/await? or some internal thing that flow has?)
Upvotes: 2
Views: 3004
Reputation: 3182
Inside generator you can call some another action at the end.
In example below I call thisIsWhatYouNeed
function. This function will be called when generator ends.
myFlow: flow(function* () {
try {
const response = yield fetch('your URL here');
const data = yield response.json()
// here you can call another action
self.thisIsWhatYouNeed(data);
} catch (error) {
console.log('error happens');
}
})
thisIsWhatYouNeed(data) {
// here you have your data and know that flow is finished
console.log('generator already finished');
}
Upvotes: 1
Reputation: 4978
a flow returns a promise, so any promise waiting mechanism works: .then
, await
, or yield
inside another flow
. If you want to render the state of the flow, take a look at mobxUtils.fromPromise(promise).case(....)
of the mobx-utils package
Upvotes: 2