代码之家  ›  专栏  ›  技术社区  ›  Max Koretskyi

我需要使用“next”或“store”吗。dispatch `在我的中间件中

  •  2
  • Max Koretskyi  · 技术社区  · 9 年前

    我正在构建我的异步中间件,我想从中分派几个操作。下面是目前的情况:

    const [ requestType, successType, failureType ] = types;
    next({type: requestType});
    
    return call().then(
        result => {
            return next({
                result,
                type: successType
            })
        },
        error =>  {
            // I want to dispath the `sendNotification()` action here
            // to add item to notifications center
            // should I use `next` or `store.dispath`?
    
            //this way?
            store.dispath(failedNotification);
            //or this way?
            next(failedNotification);
    
            return next({
                type: failureType,
                error: error.message || 'Something bad happened',
                errorType: error.errorType
            })
        }
    )
    

    我的问题在上面代码中的这些注释中:

          // I want to dispath the `sendNotification()` action here
          // to add item to notifications center
          // should I use `next` or `store.dispath`?
    

    我之所以这么问是因为根据我的理解 applyMiddleware 实施详细信息来自 here , next() 将通过剩余的中间产品链传递动作 store.dispatch 将通过整个中间产品链,包括当前的中间产品。

    1 回复  |  直到 9 年前
        1
  •  2
  •   just-boris    9 年前

    如果您使用 store.dispatch() ,调度将再次触发中间件并调用 call() 再次。有时它可能会导致无限循环,因此调用 next() 看起来这里是个更好的选择。