Created
July 7, 2021 13:20
-
-
Save charles-dr/f4bce01d03398eaf8ea4839b3713fa3f to your computer and use it in GitHub Desktop.
code revamped using promise from callback functions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const DataModel = require('./DataModel'); // let's say the target model is 'DataModel', it's based on mongoose. | |
return DataModel.findOne({ "conditionKey": "conditionValue" }) // read data from db | |
.then((data) => { // here update the data | |
data.a = "test"; | |
data.b = 1111; | |
return processData(data); // do some processing with data, let's suppose it returns (processed) data again. | |
}) | |
.then((data) => { | |
return data.save(); | |
}) | |
.then((data) => { | |
console.log("finished"); | |
}) | |
.catch((err) => { | |
console.log(err); // here catch all erros from the above steps. | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment