简体   繁体   中英

ES6 - Yield and Return at the Same Time

I have this,

function*(a){
 if (a) {
    yield API.callRoute();
    return;
  }
  // other stuff that is blocked by return
}

I want this:

function*(a){
  if (a) {
    return yield API.callRoute(); // one fewer line of code
  }
  //...
}

Doable? Why? Why not?

Yes, it's doable, but your second snippet return s the result of the yield expression. If you don't want that but make it exactly equivalent to your first snippet, use

return void yield API.callRoute();

though probably it's not worth making this a line shorter when the difference is significant.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM