本文介绍了如何管道功能,当承诺在中间的承诺检查授权?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  compose =(... fns)=> fns.reduce((f,g)=>(... args)=> f(g(... args))); 

checkAuthorization 返回一个承诺,用户被授权。
buildParams 接收 someRequestData ,并将结果管道到 searchItem

  checkAuthorization()
.then(()=> {
compose
searchItem,
buildParams
)(someRequestData)
},(e)=> {
handleError(e)
})

我认为没关系,但我希望有一个更优雅的可读性,如:

  compose(
searchItem,
checkAuthorization
buildParams
)(someRequestData)

所以会发生什么:
1)构建参数
2)checkAuth
3 )搜索项目



任何建议?

解决方案

不,因为 checkAuthorisation 不会接收并通过参数。即使你要重写它来做到这一点,仍然会很奇怪,读者会假设你正在构建应该检查授权的参数。所以不要这样做 - 你有一个非线性的流程,并试图强制它进入一些线性组合是不好的。



Btw,我建议避免

  checkAuthorization( compose  ).then(()=> 
searchItem(buildParams(someRequestData))
,e =>
handleError(e)
);

或可能

 code> checkAuthorization()然后(compose(searchItem,buildParams,()=> someRequestData)
,handleError); // ^^^^const


i'm trying to compose some functions together:

compose = (...fns) => fns.reduce((f, g) => (...args) => f(g(...args)));

checkAuthorization returns a promise that check if a user is authorized.buildParams receives someRequestData, and pipes the result to searchItem.

    checkAuthorization()
      .then(() => {
            compose(
                searchItem,
                buildParams
            )(someRequestData)
    }, (e) => {
       handleError(e)
    })

I think it's OK, but I wish to have a more elegant look for readability, something like:

    compose(
      searchItem,
      checkAuthorization
      buildParams
    )(someRequestData)

so what will happen is: 1) build params2) checkAuth3) search item

Any suggestions?

解决方案

No, that's not possible, since checkAuthorisation does not receive and pass through the params. And even if you would rewrite it to do that, it still would be weird and a reader would assume that you're building the params whose authorisation should be checked. So don't do that - you have a non-linear flow, and trying to force it into some linear composition is no good.

Btw, I would recommend to avoid compose when you're calling the function immediately anyway:

checkAuthorization().then(() =>
  searchItem(buildParams(someRequestData))
, e =>
   handleError(e)
);

or maybe

checkAuthorization().then( compose(searchItem, buildParams, ()=>someRequestData)
                         , handleError ); //                ^^^^ "const"

这篇关于如何管道功能,当承诺在中间的承诺检查授权?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-14 03:36