I have a Promise.all()
that performs two queries one after the other. In the result of the Promise.all()
I want to perform a third query using the result from the second query.
How would I create a function()
that I can cleanly use inside the Promise()
?
Promise
.all([upsertDeviceId, createEndpoint])
.then((values) => {
const upsertDeviceIdResult = values[0];
const createEndpoint = values[1];
if(upsertDeviceIdResult.upsertedCount > 0){
//Perform third query here
//performThirdQuery(createEndpoint.EndpointArn);
}
return res.status(200).send({ success: true });
})
.catch((err) => {
console.log(err);
return res.status(400).send({ reason: 'unknown' });
});
});
What I can't figure out is how can I listen to the results of this third query and
return a return res.status(200).send({ success: true });
or an err.
return res.status(500).send({ success: false});
function performThirdQuery(resultFromSecondQuery) {
const updateDeviceIdEndpointArn = db.collection('deviceids')
.updateOne(
{ device_id: deviceId },
{ $set: { device_endpoint_arn: deviceId } }
);
}