Reputation: 645
Inside my saga, I am calling an async function and passing it a callback. The reason I am passing it a callback is because it can be called a few times from the async function.
How can I call "yield put" from inside the callback which should be called more than one time from inside the async function.
Upvotes: 0
Views: 1302
Reputation: 1427
As you have noticed, there is no way to yield
inside a nested function of your generator function. Instead you can convert the callback style async function into a promise, and use the call effect.
function* generator() {
const results = yield call(function() {
return new Promise(function(resolve, reject) {
const results = [];
asyncFunction(function(result) {
if (async function is done) { resolve(results) }
else { results.push(result) }
});
});
});
yield put(action(results));
}
This is a common pattern, but the tricky part for you will be knowing when asyncFunction has completed. It will need some way to signal that it has called the callback function for the last time.
Upvotes: 1