-
Notifications
You must be signed in to change notification settings - Fork 966
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[v3] Async Transition: forward resolved value #106
Comments
Good question, I'll have to think about it. I'm not sure what the mechanism would be for forwarding the resolved/rejected value back to the user. Perhaps just storing it ephemerally as an attribute on the Also this could be complicated by the fact that, in theory, there could be multiple async steps in a single transition (you could be async during onLeaveState and during onEnterState in the same transition) so there could be multiple resolved/rejected values for a single transition. Do you have an example of how you might want your calling code to look with this feature? |
Hello! First, here is example of calling code... - current state: paymentMethod
- next state: review
// Registered hook
onLeavePaymentMethod: function(lifecycle, payload) {
return new Promise(asyn function(resolve, reject) {
// do async call to extenal service provider. Based on response, either resolve, or reject.
try {
const paymentMethodToken = validatePaymentMethod(payload);
resolve(paymentMethodToken);
} catch (e) {
reject(e);
}
})
}
// Attempt state transition
const response = await fms.review();
// Decide what to do based on response. Event better, rejected transition would throw. Calling code could be changed to following... try {
const paymentMethodToken = await fsm.review();
// e.g: store valid payment token for future use
} catch (e) {
// e.g: bail out, display validation error
} Unfortunately, I am not sure about multiple async steps, while transition from state A -> B. Do you have use case in mind? |
Actually, onLeavePaymentMethod: function(lifecycle) {
return new Promise(async function(resolve, reject) {
reject('test value');
}
}
const response = await fsm.review();
console.log(response); // 'test value' On the other hand, onLeavePaymentMethod: function(lifecycle) {
return new Promise(async function(resolve, reject) {
resolve('test value');
}
}
const response = await fsm.review();
console.log(response); // true |
@Andreyco I registered a couple of days ago a related issue before seeing yours. I'm also on branch v3. My issue was regarding the case where one of the lifecycle methods is @jakesgordon You are right that it gets complicated in the case when all lifecycle methods |
Both of these issues should be fixed in v3 branch (shortly to be released to npm as v3.0.1). Any async lifecycle event that is rejected will now throw an exception so the transition callers promise will also be rejected. Any async lifecycle event that is resolved will pass the result thru to the end of the lifecycle chain - NOTE: if you have multiple async lifecycle events in a single transition then its last-one-wins. |
@jakesgordon When I tested async resolving every async result from event handlers before |
Would it be possible to forward resolved/rejected value from async transitions?
The text was updated successfully, but these errors were encountered: