Background
I am using Fluture to abstract Futures.
Let's say I have a function that makes a GET request. This function can succeed or fail.
Upon making a request, if it succeeds, it prints a message, if it fails, it logs the error and executes a command.
axios.get(endpoint, { timeout: timeoutMs })
.fold(
err =>
logger.errorAsync( err )
.chain( ( ) => cmd.getAsync("pm2 restart app")),
response => logger.infoAsync( "Great success!" )
);
Research
I have been reading the API, and I found that bimap
and fold
both apply a function to success and error:
bimap: Maps the left function over the rejection value, or the right function over the resolution value, depending on which is present.
fold: Applies the left function to the rejection value, or the right function to the resolution value, depending on which is present, and resolves with the result.
Problem
If you have a keen eye, you will know my example doesn't work. I need to use bimap
, but I don't get why.
Questions
- When should I use
bimap
and when should I usefold
? - What are the main differences between them?