当节点被拒绝时如何继续使用相同的流?
How to continue with the same stream when a node is a rejected promise?
我正在使用 redux-observable
来发送一个转换某些承诺结果的动作。
假设我有这样一个 API 电话:
const fetchSomething = () => callAndReturnPromise('some-endpoint')
使用这些动作创建器:
const fetchSomethingSuccess = response => ({ type: 'SUCCESS', payload: response })
const fetchSomethingError = error => ({ type: 'ERROR', payload: error })
当 fetchSomething
被解决或拒绝时,我可以为这两种情况创建操作。
我希望我的史诗采取所有 'FETCH' 操作,并使用 fetchSomething
函数使用适当的操作创建者放置一个具有已解决或拒绝的承诺值的新节点。
我的史诗是这样的:
const fetchSomethingEpic = action$ =>
action$
.ofType('FETCH')
.switchMap(() =>
Observable
.fromPromise(fetchSomething())
.map(fetchSomethingSuccess)
.catch(error => Observable.of(fetchSomethingError(error)))
);
问题是发生错误时它停止工作。
当 fetchSomething
生成的 promise 被解析时,一切正常,一个新的 action 被调度,转换通过 map 传递。但是当承诺被拒绝时,在 'FETCH'
之后不会发送任何操作
--
更多信息
callAndReturnPromise('some-endpoint)
看起来像这样:
const callAndReturnPromise = endpoint => new Promise((resolve, reject) => {
fetch(endpoint)
.then(response => {
if (checkIsCool(response)) {
resolve(response);
} else {
reject(response)
}
})
})
调试后我得到这样的结果,让我们用 F、S、E(获取、成功和错误)命名流中的操作
----F----S------F-------F------S
^ there occurred an error but no action was sent
The problem is that it stops working when an error occurs
能具体点吗?究竟是什么停止工作,你怎么知道?有没有错误?等等
我确实立即看到了一个问题,但这可能是一个解析错误,所以它可能只是您问题中的一个错字:从箭头函数返回一个对象需要将大括号括在括号中。
// before
const fetchSomethingSuccess = response => { type: 'SUCCESS', payload: response }
const fetchSomethingError = error => { type: 'ERROR', payload: error }
// after
const fetchSomethingSuccess = response => ({ type: 'SUCCESS', payload: response })
const fetchSomethingError = error => ({ type: 'ERROR', payload: error })
虽然在这种特定情况下它会是一个解析错误,但在某些情况下它实际上会默默地解析为带有 labeled statement 的块,这对许多人来说非常混乱。
例如这是完全有效的 JavaScript 并且不是解析错误:
const fetchSomethingSuccess = response => { type: 'SUCCESS' }
在这种情况下,{ type: 'SUCCESS' }
是一个带有无关的字符串标记语句的块,这两个语句都不会以任何方式使用或返回。认为它在功能上等同于此:
const fetchSomethingSuccess = function (response) {
type: 'SUCCESS'; // extraneous
// no return
};
根据我们的进一步对话和新提供的代码:
我将提供的代码添加到 jsbin 中,它似乎按我预期的那样工作,ERROR
操作已正确调度。 https://jsbin.com/rujemak/edit?js,output
我正在使用 redux-observable
来发送一个转换某些承诺结果的动作。
假设我有这样一个 API 电话:
const fetchSomething = () => callAndReturnPromise('some-endpoint')
使用这些动作创建器:
const fetchSomethingSuccess = response => ({ type: 'SUCCESS', payload: response })
const fetchSomethingError = error => ({ type: 'ERROR', payload: error })
当 fetchSomething
被解决或拒绝时,我可以为这两种情况创建操作。
我希望我的史诗采取所有 'FETCH' 操作,并使用 fetchSomething
函数使用适当的操作创建者放置一个具有已解决或拒绝的承诺值的新节点。
我的史诗是这样的:
const fetchSomethingEpic = action$ =>
action$
.ofType('FETCH')
.switchMap(() =>
Observable
.fromPromise(fetchSomething())
.map(fetchSomethingSuccess)
.catch(error => Observable.of(fetchSomethingError(error)))
);
问题是发生错误时它停止工作。
当 fetchSomething
生成的 promise 被解析时,一切正常,一个新的 action 被调度,转换通过 map 传递。但是当承诺被拒绝时,在 'FETCH'
--
更多信息
callAndReturnPromise('some-endpoint)
看起来像这样:
const callAndReturnPromise = endpoint => new Promise((resolve, reject) => {
fetch(endpoint)
.then(response => {
if (checkIsCool(response)) {
resolve(response);
} else {
reject(response)
}
})
})
调试后我得到这样的结果,让我们用 F、S、E(获取、成功和错误)命名流中的操作
----F----S------F-------F------S
^ there occurred an error but no action was sent
The problem is that it stops working when an error occurs
能具体点吗?究竟是什么停止工作,你怎么知道?有没有错误?等等
我确实立即看到了一个问题,但这可能是一个解析错误,所以它可能只是您问题中的一个错字:从箭头函数返回一个对象需要将大括号括在括号中。
// before
const fetchSomethingSuccess = response => { type: 'SUCCESS', payload: response }
const fetchSomethingError = error => { type: 'ERROR', payload: error }
// after
const fetchSomethingSuccess = response => ({ type: 'SUCCESS', payload: response })
const fetchSomethingError = error => ({ type: 'ERROR', payload: error })
虽然在这种特定情况下它会是一个解析错误,但在某些情况下它实际上会默默地解析为带有 labeled statement 的块,这对许多人来说非常混乱。
例如这是完全有效的 JavaScript 并且不是解析错误:
const fetchSomethingSuccess = response => { type: 'SUCCESS' }
在这种情况下,{ type: 'SUCCESS' }
是一个带有无关的字符串标记语句的块,这两个语句都不会以任何方式使用或返回。认为它在功能上等同于此:
const fetchSomethingSuccess = function (response) {
type: 'SUCCESS'; // extraneous
// no return
};
根据我们的进一步对话和新提供的代码:
我将提供的代码添加到 jsbin 中,它似乎按我预期的那样工作,ERROR
操作已正确调度。 https://jsbin.com/rujemak/edit?js,output