0

My case is as follows:

function * uploadFile({file}) {
    Papa.parse(file, {
        header: true,
        complete: function(results) {
            console.log(results.data);
            //if errors => call uploadFailed action with yield
            //else yield put(uploadSucceed)
        }
    });
}

The commented code cannot be called because it's not a generator function. If I add the * to the callback definition, there's no error but it doesn't work.

I've also tried wrapping it in another function that accepts a callback without any luck. Walked through several answers like 1 2 but these aren't my case. Also there's no documentation on Papa with redux as far as I've come to see. Any help would be appreciated.

BTW:

complete: yield call(someAction) //doesn't work as well.
Moshisho
  • 2,781
  • 1
  • 23
  • 39

1 Answers1

2

Promisify parsing function and use call effect

function parse(file) {
  return new Promise((complete, error) => {
    Papa.parse(file, {complete, error})
  })
}

function * uploadFile({file}) {
    try {
      const result = yield call(parse, file);

      // yield put success action

    } catch(error) {

      // yield put error action
    }

}
Yury Tarabanko
  • 44,270
  • 9
  • 84
  • 98