Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between yield [] & yield all() - ES6/redux-saga

Is there any advantage in using redux-saga's yield all([]) over ES6's built-in yield []?

To run multiple operations in parallel, redux-saga suggests:

const result = yield all([
  call(fetchData),
  put(FETCH_DATA_STARTED),
]);

But the same can be accomplished without the all() method:

const result = yield [
  call(fetchData),
  put(FETCH_DATA_STARTED),
];

Which one is better & why?

like image 793
Ali Saeed Avatar asked Dec 20 '17 14:12

Ali Saeed


People also ask

What is the difference between yield and dividend yield?

The Difference Between Yield and Dividend Yield The key difference between yield and dividend yield is that yield measures an investment's current return, while dividend yield measures a stock's current dividend payout.

What's the difference between yield and interest rate?

Yield is the annual net profit that an investor earns on an investment. The interest rate is the percentage charged by a lender for a loan. The yield on new investments in debt of any kind reflects interest rates at the time they are issued.

Is yield the same as return Python?

There are two major differences between the working of yield and return statements in python. Return statement stops the execution of the function. Whereas, yield statement only pauses the execution of the function. The statements written in a program after the return statement are unreachable and are never executed.

What is the difference between yield and IRR?

The Yield function is helpful for tracking interest income on bonds. Whereas IRR simply calculates interest rate gains, Yield is best suited for calculating bond yield over a set period of maturity.


1 Answers

There's no functional difference, as Mateusz Burzyński (redux-saga maintainer) explains here:

Under the hood they are both the same, yield [...effects] will result in a deprecation warning though and inform you about all.

This was introduced to make parallel behaviour explicit and it nicely mirrors Promise.all

It's preferred to use all() as it informs the reader that we're yielding more than 1 effect here, but the various uses of yield will still work without it:

yielding an object with multiple effects

const { company, profile } = yield {
  company: select(getCompany),
  profile: select(getUserProfile, userId),
};

yielding an array literal

yield [
  put(userRequestSucceeded(userId)),
  put(userReceived(response.data)),
];

yielding an array using map

yield userIds.map(userId => call(fetchUserDetails, userId));
like image 174
Ali Saeed Avatar answered Oct 11 '22 22:10

Ali Saeed