How would you functional people rewrote this code? The main goal is as follow :
• fetch data from db
• if we are offline
◦ we get an error from the database, emit the error, we’re done.
◦ we get data from the database, emit the data, we’re done.
• if we are online and get an error from the database
◦ fetch the data from the api
◦ if we also get an error, emit the api error, we’re done.
◦ if we get a value, emit it and save it in database, we’re done.
• if we are online and get a value from the database
◦ emit the data from the database
◦ fetch the data from the api
◦ if we get an error, emit the api error, we’re done.
◦ if we get a value from the api and we should update and the value from the database is different than the one from the api, emit the api data and save it in database