Wait for afterCreate flow to finish #1673
Answered
by
EmilTholin
EmilTholin
asked this question in
Q&A
-
I would like to run some code after a const Model = types.model("Model", {}).actions((self) => {
return {
afterCreate: flow(function* () {
try {
yield persist("xxxxxxxxx", self, {
storage: AsyncStorage,
jsonify: true,
});
} catch (error) {
// ...
}
}),
};
});
const m = Model.create();
// run some code after the afterCreate flow has resolved... |
Beta Was this translation helpful? Give feedback.
Answered by
EmilTholin
Mar 24, 2021
Replies: 1 comment
-
You can store the promise that is the result of the const Model = types.model("Model", {}).actions((self) => {
let afterCreatePromise;
const afterCreate = flow(function* () {
try {
yield persist("xxxxxxxxx", self, {
storage: AsyncStorage,
jsonify: true,
});
} catch (error) {
// ...
}
});
return {
afterCreate() {
afterCreatePromise = afterCreate();
return afterCreatePromise;
},
clear: flow(function* () {
yield afterCreatePromise;
// do some clearing...
}),
};
});
const m = Model.create();
// ...
m.clear().then(() => {
// cleared!
}); |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
EmilTholin
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You can store the promise that is the result of the
afterCreate
flow in a volatile state variable, and add a separate method to the model that runs the code once theafterCreate
promise has resolved: