question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to yield inside a forEach

See original GitHub issue

Say for example we have a forEach loop which needs to yield.

function* mySaga() {
  [1,2,3].forEach(function* (x) {
   yield call(foo, bar); 
  });
}

What syntax would you use in mySaga to denote it to the middleware?

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:21
  • Comments:12 (4 by maintainers)

github_iconTop GitHub Comments

328reactions
yelouaficommented, Jul 28, 2017

Use map to yield a parallel effect. If you want to yield effect in sequence (wait for 1st effect to terminate before yielding the 2nd) use a normal for loop

function* mySaga() {
  yield all([1,2,3].map(x => call(foo, bar)));
}
22reactions
Andaristcommented, Feb 23, 2017

just do the simplest thing

function* safeSomeSaga(item) {
	const task = yield fork(someSaga, item);
	
	const { error } = yield race({
		success: take(SUCCESS),
		error: take(FAILURE),
	})
	
	if(error) {
		yield cancel(task)
	}	
}

function* example {
	yield items.map(item => call(safeSomeSaga, item))
}

ive corrected your example a little bit

  1. you need to use fork instead of call to achieve a non-blocking effect which will return a task descriptor
  2. and ive used a race instead of a take with array pattern, it feels more natural for me and gives more meaning for that part of the code
Read more comments on GitHub >

github_iconTop Results From Across the Web

javascript - Can I yield from an inner function? - Stack Overflow
No, you can't yield from a callback (technically, it's not an "inner function", which ...
Read more >
yield statement - provide the next element in an iterator
yield return : to provide the next value in iteration, as the following example shows: C# Copy. Run. foreach (int i in ProduceEvenNumbers(9))...
Read more >
generators vs forEach - ESDiscuss.org
The support for nested generators ("yield*") differs from normal function call operation. ... forEach(Array, GeneratorFunction) with yield* in the body.
Read more >
yield - JavaScript - MDN Web Docs - Mozilla
The yield operator is used to pause and resume a generator function.
Read more >
How to Use Generators and yield in Python
But now, you can also use it as you see in the code block above, where i takes the value that is yielded....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found