最外部的.catch()是否可用于所有链式嵌套的promiss?

最外部的.catch()是否可用于所有链式嵌套的promiss?,第1张

最外部的.catch()是否可用于所有链式/嵌套的promiss?

最后,您会看到一个catch语句,我的问题是,该catch语句是否能满足所有诺言?

不,它仅适用于外部承诺,即

then
调用返回的承诺。需要将其拒绝
catch
才能激活回调。要拒绝此承诺,
apiAccountStatus(…)
必须拒绝或
then
回调必须抛出异常或
返回将被拒绝的承诺

最后一件事就是您所缺少的-您在

then
回调中创建了更多的Promise ,但是您没有使用
return
它们,因此它们不会链接。你所要做的

export function authenticationSignIn(email, password) {  return (dispatch) => {    dispatch({ type: AUTHENTICATION_REQUEST });    apiAccountStatus(email, password)    .then(({data: {status}}) => {      if (status === 'ACCOUNT_CREATED') {        return apiSignIn(email, password)//      ^^^^^^        .then(({ data: sessionData }) => {          return apiIndexAccounts()//        ^^^^^^          .then(({ data: accountsData }) => { dispatch({ type: AUTHENTICATION_SUCCESS }); window.router.transitionTo('/dashboard/home');          });        });      } else if (status === 'SOMETHING ELSE') {        // TODO: HANDLE SOMETHING ELSE      }    })    .catch(({ response }) => {      dispatch({ type: AUTHENTICATION_FAILURE });      dispatch(notificationShow('ERROR', response.data.type));    });  };}


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/5643153.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-16
下一篇 2022-12-16

发表评论

登录后才能评论

评论列表(0条)

保存