Questions tagged [es6-generator]

Whenever you have a question about ES6 generator, or a question strongly related to it, this is an appropriate tag.

Javascript generators are special functions, which can be paused with the keyword yield, which can be used to establish a 2-way communication between the generator and its caller.

Generator function declaration:

function *myGenerator(params) {/*...*/}

The star (*) character before myGenerator() above means that the function is a generator.

Generator function expression:

const myGenerator = function *(params) {/*...*/}

When using a function expression to create a generator the star (*) between the function keyword and the opening parenthesis above means that the function is generator.

We can assign the function to a variable, but this is not starting the function, anything passed as a parameter will be ignored:

var myExecutor = myGenerator();

myExecutor is an iterable, which steps through the generator, stopping at each yield.

myExecutor.it(someparams)

resumes the generator from the last yield where it stopped previously (or the start of the function) and it executes the function until the next yield, or, in the case when there is no next yield, until the end of the function.

18 questions
6
votes
4 answers

React: make a cycle-through component with both forward and backward directions using Generator

I have an array of strings to display const array = ["one", "two", "three"]; . The UI initially shows the first item in the array i.e. "one". From there I have a button right when clicked it shows the next item or string which is two, and then…
Joji
  • 4,703
  • 7
  • 41
  • 86
4
votes
1 answer

Is there a javacript async equivalent of python zip function?

There is an async iterable class Fasta { //read file line by line and yield a class based on every four lines constructor(path) { this.path = path const filestream = fs.createReadStream(this.path) if…
2
votes
1 answer

How to pass async generator through MessageChannel?

I have the following code which does what I want to: function remoteGenerator(port) { const createPromise = () => { let handlers; return { promise: new Promise( (resolve, reject) => (handlers = { resolve, reject }) ), …
Eugene
  • 63
  • 5
2
votes
2 answers

Function generator with Promise

I have to write async function for: const myAsyncFunction = async(function* (promise) { const data = yield promise; console.log(data); }); myAsyncFunction(Promise.resolve("Hello world")); // console: ‘Hello world!’` result should be - console:…
kuka
  • 663
  • 1
  • 5
  • 13
2
votes
4 answers

Why does my generator become empty after being iterated through?

I have a generator being returned to me by a function call from a library I'm using. I then pass this generator to a function which iterates through it and does a bunch of logic on each of the items. I then want to refer to this same generator after…
TomLisankie
  • 3,785
  • 7
  • 28
  • 32
2
votes
1 answer

Calling generator function from setTimeout

The following js code fails in developer console of firefox, chrome and nodejs as well. Unable to figure out why? function* x() {} let y = x() setTimeout(y.next, 100) Error in firefox TypeError: CallGeneratorMethodIfWrapped method called on…
chinoy
  • 172
  • 1
  • 13
1
vote
1 answer

JavaScript: using Generator to make Binary Search Tree In order Iterator

I am trying to solve this leetcode question https://leetcode.com/problems/binary-search-tree-iterator/ where it asks you to make an iterate to traverse the BST and I thought generators are a good fit for it. Here is my attempt class BSTIterator { …
Joji
  • 4,703
  • 7
  • 41
  • 86
1
vote
0 answers

Waiting for multiple actions before proceeding in redux-saga

Assume that you have a saga that fetches a list of items. For each fetched item you also need a companion object. The pseudo-code below fetches some users from an API and for each user is emitting a new action to fetch her avatar. function*…
antoniom
  • 3,143
  • 1
  • 37
  • 53
1
vote
1 answer

Error using a generator function as value of a WeakMap

I'm building a linked list by my own. I tried to assign a generator as value of a key/value WeakMap in the constructor. The _iterator is a WeakMap because is a private member that I want use to make simple iterate over my data structure, but I don't…
Nick
  • 1,439
  • 2
  • 15
  • 28
1
vote
1 answer

Is there a way to reduce the number of yields in an infinite generator communicating with its caller?

At a discussion about Javascript generators someone cooked up an interesting function: function *foo() { var y = 1; while(true) yield (y = (y * (yield))); } Now, this function can be used via: var results = []; var bar =…
Lajos Arpad
  • 64,414
  • 37
  • 100
  • 175
0
votes
1 answer

Print every value within range iterator generator function javascript

Iterator should print every value within range but its only printing alternate nos. function iterator(rangeStart, rangeEnd) { if (rangeStart == 0 && rangeEnd == 0) { return null; } var iterate = function*(start = 0, end = 5, step = 1) { …
0
votes
1 answer

how to use while loop in generator function

i'm new to generator function and trying execute while loop inside it. export function* findRandomData(list, name) { let searchList = true; const formattedName = name .replace(new RegExp('_', 'g'), ' ') .toLowerCase(); const…
Nikhil Shrestha
  • 1,210
  • 1
  • 11
  • 18
0
votes
3 answers

Calling API using generators, next() functions returningPromise, was expenting the response

I have 2 questions to ask, regarding generators, since I am just learing this feature. Not sure, what is wrong in the below implementation. I was expenting the output to be { "userId": 1, "id": 1, "title": "delectus aut autem", …
RONE
  • 5,415
  • 9
  • 42
  • 71
0
votes
0 answers

How to create Redux Saga that returns a payload

Hello I would like to know what is wrong with my code: Even though my ajax request works, the payload object is always undefined. If I understand sagas correctly the fetchWord function should wait for the fetchWordRequest promise to be resolved,…
0
votes
1 answer

Javascript Generator: how to get an object as parameter to generate another object?

I'm trying to get a "template" object as input to a generator, in order to get other objects. I would be able to change the template just before each generation. It's almost working as expected, but I'm having the following issue. I just put…
João Otero
  • 948
  • 1
  • 15
  • 30
1
2