ES6 give multiple generators
In ES6, the output and generator function allow you to wait for the function to execute. But I want to wait for multiple generators. Here's the code:
files.forEach(function* (file) {
const uploadedFile = yield call([service, service.upload], file, config)
}
call
redux-saga effect
To express the logic of the Saga, we give simple JavaScript objects from the generator. We call these object effects
I want to start all downloads in one go without waiting for the previous one to finish and wait for all the files to be downloaded, is this possible with yield
?
source to share
What I was looking for was actually:
// correct, effects will get executed in parallel
const [users, repos] = yield [
call(fetch, '/users'),
call(fetch, '/repos')
]
call
that's just a promise
When we receive an array of effects, the generator blocks until all effects are removed or as soon as someone is rejected (how and how Promise.all works).
source to share
You can use Promise.all () instead of
Example:
If you want to start all uploads and regain control of the code when they are finished, you can use the async / await interface:
function uploadALL(files){
const uploadedFilesPromises = files.map( file => {
return call([service, service.upload], file
})
}
return Promise.all(uploadedFilesPromises);
}
// samples of using this function:
uploadALL(files).then((uploadedFiles)=> { .... })
// or in async function you can use await:
const uploadedFiles = await uploadAll(files)
Your "invocation" method must return a Promise object, otherwise you must move it to a Promise.
source to share