Anees Hikmat Abu Hmiad
Anees Hikmat Abu Hmiad

Reputation: 3560

ES6 Generators not working correctly when send arguments by next()

Im not understand the idea, wrong was happened in this code:

Image Code

Output always undefined for first item...(Also if I set another yieldData5.next(...) after last line of code, it will be print on console

(undefined

bye : taher

undefined)

thanks,

Upvotes: 1

Views: 73

Answers (2)

Felix Kling
Felix Kling

Reputation: 816970

Output always undefined for first item

You are not passing anything to the function, so i is undefined and therefore yield i; is equivalent to yield undefined;. You are explicitly yielding undefined from the function, and that's what you see.


If your question is how to reference the value passed to the first call of .next, the answer is unfortunately: You can't.

This is being addressed in the next version of ECMAScript.

Upvotes: 2

Alexandr Lazarev
Alexandr Lazarev

Reputation: 12892

Try:

let yieldData5 = collection_name5({index: 0, masg: 'Hi');
console.log(collection_name5.next({index: 2, masg: 'bye').value);

Upvotes: 0

Related Questions