我是函数式编程的新手,我发现了以下 Fluture 函数式编程示例,它似乎为处理数据库查询和后续数据操作提供了一个非常好的示例。然而需要注意的是,在阅读函数式编程概念时,Just/Nothing monad 似乎是处理空值检查的建议方法。1)这如何适合这个例子和 2)如果 findOne 被拒绝,它会阻止后续链运行并立即进入分叉吗?
import Future from 'fluture';
const processAll = Future.fork(_sendError, _sendResponse);
const _fetchFromDB =
encaseP(userId => myModel.findOne({ id: userId }).exec())
//Future.fromPromise(userId => myModel.findOne({ id: userId }).exec())
processAll(_fetchFromDB(userId)
.chain(getDataGeneric)
.chain(_findDevice)
.chain(_processRequest))
我从以下 stackoverflow 链接中获得了这个示例,并将 fromPromise 修改为 encaseP:
如何让 Either Monads 了解异步函数(Promises/Future)
我假设 encaseP 在将 Promise 转换为 Future 的示例中将替换 fromPromise。