{"action":"created","issue":{"url":"https://api.github.com/repos/yelouafi/redux-saga/issues/613","repository_url":"https://api.github.com/repos/yelouafi/redux-saga","labels_url":"https://api.github.com/repos/yelouafi/redux-saga/issues/613/labels{/name}","comments_url":"https://api.github.com/repos/yelouafi/redux-saga/issues/613/comments","events_url":"https://api.github.com/repos/yelouafi/redux-saga/issues/613/events","html_url":"https://github.com/yelouafi/redux-saga/issues/613","id":187267860,"number":613,"title":"Handle promise response and event callback together","user":{"login":"dimapaloskin","id":5237085,"avatar_url":"https://avatars.githubusercontent.com/u/5237085?v=3","gravatar_id":"","url":"https://api.github.com/users/dimapaloskin","html_url":"https://github.com/dimapaloskin","followers_url":"https://api.github.com/users/dimapaloskin/followers","following_url":"https://api.github.com/users/dimapaloskin/following{/other_user}","gists_url":"https://api.github.com/users/dimapaloskin/gists{/gist_id}","starred_url":"https://api.github.com/users/dimapaloskin/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/dimapaloskin/subscriptions","organizations_url":"https://api.github.com/users/dimapaloskin/orgs","repos_url":"https://api.github.com/users/dimapaloskin/repos","events_url":"https://api.github.com/users/dimapaloskin/events{/privacy}","received_events_url":"https://api.github.com/users/dimapaloskin/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":2,"created_at":"2016-11-04T07:15:43Z","updated_at":"2016-11-04T10:53:26Z","closed_at":null,"body":"Imagine that there is next function:\r\n\r\n```js\r\nfunction asyncTask(callback) {\r\n  return new Promise((resolve, reject) => {\r\n\r\n    if (callback) {\r\n      setTimeout(() => {\r\n        callback(50);\r\n      }, 100);\r\n\r\n      setTimeout(() => {\r\n        callback(80);\r\n      }, 200);\r\n    }\r\n\r\n    setTimeout(() => {\r\n      resolve('result');\r\n    }, 500);\r\n  });\r\n}\r\n```\r\nIt's similar with axios workflow when I need upload file and catch progress event.\r\nHow I can get the promise result and also handle progress event?\r\nFor example something like that:\r\n\r\n```js\r\n\r\nfunction* uploadSource(action) {\r\n\r\n  const result = yield call(asyncTask, (data) => {\r\n    put({ type: 'PROGRESS', payload: data });\r\n  });\r\n  put({ type: 'SUCCESS', payload: result });\r\n}\r\n```\r\n\r\nSure, last code example is not working. I tried use the eventChannel, but without any success result.\r\n"},"comment":{"url":"https://api.github.com/repos/yelouafi/redux-saga/issues/comments/258401094","html_url":"https://github.com/yelouafi/redux-saga/issues/613#issuecomment-258401094","issue_url":"https://api.github.com/repos/yelouafi/redux-saga/issues/613","id":258401094,"user":{"login":"dimapaloskin","id":5237085,"avatar_url":"https://avatars.githubusercontent.com/u/5237085?v=3","gravatar_id":"","url":"https://api.github.com/users/dimapaloskin","html_url":"https://github.com/dimapaloskin","followers_url":"https://api.github.com/users/dimapaloskin/followers","following_url":"https://api.github.com/users/dimapaloskin/following{/other_user}","gists_url":"https://api.github.com/users/dimapaloskin/gists{/gist_id}","starred_url":"https://api.github.com/users/dimapaloskin/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/dimapaloskin/subscriptions","organizations_url":"https://api.github.com/users/dimapaloskin/orgs","repos_url":"https://api.github.com/users/dimapaloskin/repos","events_url":"https://api.github.com/users/dimapaloskin/events{/privacy}","received_events_url":"https://api.github.com/users/dimapaloskin/received_events","type":"User","site_admin":false},"created_at":"2016-11-04T10:53:26Z","updated_at":"2016-11-04T10:53:26Z","body":"@Andarist thank you, it's help. But I added few changes.\r\nFor now it's looks like:\r\n\r\n```js\r\n\r\nfunction asyncTask(callback) {\r\n\r\n  if (callback) {\r\n    setTimeout(() => {\r\n      callback('progress');\r\n    }, 300);\r\n  }\r\n\r\n  return new Promise((resolve) => {\r\n    setTimeout(() => {\r\n      resolve('finish');\r\n    }, 500);\r\n  });\r\n}\r\n\r\nfunction createAsyncTaskRunner(data) {\r\n\r\n  let emit;\r\n  const chan = eventChannel(emitter => {\r\n      emit = emitter\r\n      return () => {}\r\n  });\r\n\r\n  const promise = asyncTask((data) => {\r\n    emit('PROGRESS');\r\n    /*\r\n       stop if 100% uploaded (using inside project) \r\n       if (data && data.progress === 1) {\r\n        emit(END);\r\n      }\r\n    */\r\n  });\r\n\r\n\r\n  return [ promise, chan ];\r\n}\r\n\r\n\r\nfunction* watchOnProgress(chan) {\r\n  while (true) {\r\n      const data = yield take(chan);\r\n      yield put({ type: 'PROGRESS', payload: data });\r\n  }\r\n}\r\n\r\nfunction* uploadSource(action) {\r\n  const [ promise, chan ] = createAsyncTaskRunner('test');\r\n  yield fork(watchOnProgress, chan);\r\n  try {\r\n    const result = yield call(() => promise);\r\n    put({ type: 'SUCCESS', result });\r\n  } catch (err) {\r\n    put({ type: 'ERROR' });\r\n  }\r\n}\r\n``` \r\n\r\nThanks a lot!"}}
{"actor":{"display_login":"dimapaloskin"}}