Currently, I am working on creating a Thennable function that may return a promise based on its parameters. However, there is a possibility that the parameters are invalid which would require breaking the promise chain with something unexpected. What can be used in this scenario? Here is a rough outline of my current function:
function getThumbnailFromClips(res, clips) {
var clips = content.clips;
if (clips.length == 0) {
res.status(400).send(new Error("Empty playlist"))
// >> WHAT SHOULD BE RETURNED HERE? <<
} else {
var pick1 = Math.floor(Math.random() * clips.length);
var sql1 = ` SELECT <details omitted> `;
let promise = client.query(sql1)
return promise
}
}
This function is typically called from an app.post callback like so:
app.post("/foo", (req,res)=>{
getClips(req)
.then((clips)=>getThumbnailFromClips(res,clips))
.then(((result)=>createDatabaseEntry(result)))
.catch(/*etc*/)
})
Also, should it be considered better practice to handle sending the 400 result code within the outermost caller instead?