Replies: 3 comments 2 replies
-
Flow transforms a sync generator function into an async function: increase() {
this.itemCount++;
}
// async keyword removed
*increaseLot() {
this.itemCount++;
yield Promise.resolve(true);
this.itemCount++;
yield this.increaseInner(); // increaseInner returns Promise not a generator, therefore only yield without *
this.itemCount++;
}
// async keyword removed
*increaseInner() {
this.itemCount++;
this.itemCount++;
yield Promise.resolve(true);
this.itemCount++;
this.itemCount++;
} |
Beta Was this translation helpful? Give feedback.
-
Thank you @urugator . Removing the async modifier as you suggested helped, but you still need yield* to call another generator from the first generator. So the correct code is the following, which prints the correct sequence. I have added comments for clarity. Thanks again for the help.
|
Beta Was this translation helpful? Give feedback.
-
@urugator - True, if I add the flow annotation to the inner then it will iterate through it and I don't need yield*. I went down this whole rabbit hole because originally I marked all my flow functions as async, but not making them async and marking them all as flow function produces the same output with much cleaner code. Thanks. |
Beta Was this translation helpful? Give feedback.
-
I have a flow function with the usual yields in it. I want to call another function from it that also has yields, so I'm using yield*. However, after that, MobX doesn't wrap the state changing blocks into actions correctly and produces the warning "Since strict-mode is enabled, changing (observed) observable values without using an action is not allowed".
Interestingly, adding an empty yield after the yield* solves the problem. Here is a sample code, commenting or uncommenting a line in the increaseLot() function change shows/hides the error.
What is the correct way to call another function from a flow function? I also tried to add flow annotation to the inner function, but that produces JavaScript errors down the line.
Beta Was this translation helpful? Give feedback.
All reactions