Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

在 effects 里用 call 调用的异步操作,参数里有一项是回调,在回调里 put action #733

Closed
mdluo opened this issue Mar 15, 2017 · 4 comments

Comments

@mdluo
Copy link

mdluo commented Mar 15, 2017

@sorrycc

比如,参考 JakeChampion/fetch#89 (comment) 这里写了个带进度回调的上传异步函数 futch,然后在 effects 里

yield call(futch, '/', {}, (event) => {
  put({ type: 'progress', payload: event })
})

这样是不行的,或者写成

yield call(futch, '/', {}, function *(event) {
  yield put({ type: 'progress', payload: event })
})

那在 futch 里怎么触发这个 Generator 函数并传入参数?

@mdluo
Copy link
Author

mdluo commented Mar 15, 2017

解决了,用第二种写法,传入 Generator 函数,在 futch

xhr.upload.onprogress = (event) => {
  onProgress(event).next()
}

@mdluo
Copy link
Author

mdluo commented Mar 15, 2017

但是仍然有问题,yield put({ type: 'progress', payload: event }) 这个 Action 无法被对应的 reducer 接收到

@mdluo
Copy link
Author

mdluo commented Mar 15, 2017

已完美解决

参考了 #322 提到的 https://stackoverflow.com/questions/40685288/redux-saga-upload-file-progress-event

使用了 axios,在 Component 里直接发起请求并解析请求的 Promise,在 onUploadProgress 回调里 dispatch 上传进度的消息,使用 CancelToken 来取消上传

这个需求在 effects 里确实不好做

@hudianlao48
Copy link

@mdluo 在Component里直接发起请求感觉是跳过了saga的effects……我感觉解决方法不完美啊。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants