Rxjs / Reactive Programming / redux observable 中的全局变量使用

Global Variable usage in Rxjs / Reactive Programming / redux observable

假设我正在编写如下 rxjs 史诗:

const epic = (action$) => {
   const id = nanoid(); # function that generate uuid

   return action$.pipe(
       switchMap(action => doApiCall(id)).pipe(
           map(response => response.data)
           map(response => addToast(id)),
           catchError(error => addErrorToast(id))
       )
   )
}

因为这个史诗将链接在史诗中间件中,所以每次发出事件时都不会生成 uuid

如何使 nanoid 事件在事件发出时生成并将其传递给响应和错误捕获块

谢谢!

不确定我是否理解这个问题,但是如果每次 action$ 发出时你都想要一个新的 id 值,你可以这样做:


const epic = (action$) => {


  return action$.pipe(
        switchMap(action => {
            const id = nanoid(); // function that generate uuid
            return doApiCall(id).pipe(
                map(response => response.data),
                map(response => addToast(id)),
                catchError(error => addErrorToast(id))
            )
        })
    )
}