I have a pretty simple question with respect to setting status codes after a database write. Let's say you're saving a new document to an object collection with something like mongoose. The following is inside a POST handler to some route, after some validation logic:
MyModel.create({
prop: dataFromPost
}).then(
() => {console.log("Success");}, // status => 201
err => {console.log(`Error: ${err}`);} // status => 5xx ???
);
response.writeHead(202);
Is this the proper approach? It just seems as tho if for some reason the write failed, then responding with 201 synchronously would be obviously the wrong thing to do.
Any advice with respect to handling this type of asynchronous branching in response flows would be greatly appreciated.