Skip to content

Instantly share code, notes, and snippets.

@tomkis
Created February 12, 2016 17:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tomkis/951fd36d485243f5ca71 to your computer and use it in GitHub Desktop.
Save tomkis/951fd36d485243f5ca71 to your computer and use it in GitHub Desktop.
import api from './path/to/api'
import { take } from 'redux-saga'
function* watchSave() {
while(true) {
const { data } = yield take('SAVE_DATA');
// Just naive example that callbacks do not propagate yield*
yield* data
.forEach(function*(record) {
yield api.save(record)
}); // this may be some non-trivial non-generator friendly function
}
}
@yelouafi
Copy link

Not sure to understand why do you have to dispatch an observable to the store

Shouldn't you rather listen to the observable from the outside and dispatch incoming occurrences to the store

data.forEach(ev => dispatch(ev))

function* watchSave() {
  while(true) {
    const { record } = yield take('SAVE_DATA_RECORD');
    yield fork(saveData, record)
  }
}

@slorber
Copy link

slorber commented Feb 13, 2016

yield data.forEach(function*(record) {
        yield api.save(record)
      }); 

why not using map instead???

@tomkis
Copy link
Author

tomkis commented Feb 15, 2016

@slorber we've been discussing this with @yelouafi on twitter. You guys probably missed my pain point and it's the fact that you simply can't use HoF with generators. Of course, I am aware that it can be mapped over but this is a trivial use case - there'r many use cases when you simply have a lot of code implemented which heavily relies on callbacks - and this can't be used because callbacks do not propagate yield* automatically.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment