添加链接
link之家
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

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?)

What do you mean render the state of the flow? Also if I am using Axios and yield it. do I return it's response and that gets wrapped in a promise by "flow"? – chobo2 Jul 23, 2018 at 5:33 A flow is a promise in itself, so if you want render it's state during the promise transitions, the above util comes in handy. – mweststrate Jul 24, 2018 at 13:19 but do I have to await the actual flow method? I want to do stuff after myFlow is called and finished. I want to either capture an error and update the UI or capture the success and update the UI but I want to do it outside of the flow call. – chobo2 Jul 24, 2018 at 16:12 any flow method returns a promise. So you can await that, then it, etc: myObject.myFlow().then(() => { console.log('done!') } – mweststrate Jul 26, 2018 at 13:38

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');
        

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.