I am trying to use Either Monad to pipe my data through, the problem is that I can't figure out how to make my Monad to be aware of the operation that is async
here is what I have
let processData = Either.either(_sendError, _sendResponse)
processData(_getDataGeneric(queryResult)
.chain(_findDevice)
.chain(_processRequest)
);
queryResult is what I fetch from the database itself.
the problem is that fetching the result is only in the middle of the pipeline. what I want is this
ValidateUserInput -> GetDataFromDB -> ProcessData
processAll(_fetchFromDB(userId)
.getDataGeneric
.chain(_findDevice)
.chain(_processRequest))
//_fetchFromDB , Mongoose Query
function _fetchFromDB(userId){
return myModel.findOne({id:userId}).exec()
.then(function(result){
return Right(result)
}).catch((err)=>Left(err))
}
if result is valid from DB,it will return an Right instance, and if there is any sort of error, it will return Left
the problem is that this operation is Async, and i am not sure how to get my Either Monad to handle it and process it.
Any ideas on how to make my Monad aware of Promises in its operation?