簡體   English   中英

ES6-同時收益和收益

[英]ES6 - Yield and Return at the Same Time

我有這個,

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

我要這個:

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

可行嗎 為什么? 為什么不?

是的,這是可行的,但是您的第二個代碼段return的是yield表達式的結果。 如果您不希望這樣做,但使其與第一個代碼段完全相同,請使用

return void yield API.callRoute();

盡管當差異顯着時,使這行短一點是不值得的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM