Skip to content

Instantly share code, notes, and snippets.

@charles-dr
Created July 7, 2021 13:20
Show Gist options
  • Save charles-dr/f4bce01d03398eaf8ea4839b3713fa3f to your computer and use it in GitHub Desktop.
Save charles-dr/f4bce01d03398eaf8ea4839b3713fa3f to your computer and use it in GitHub Desktop.
code revamped using promise from callback functions
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