I'm trying to get my head around accessing previous results in promises as outlined here How do I access previous promise results in a .then() chain?
I've managed to get this working with 2 promises, but when I add a 3rd that makes a POST request to an API the promise result is my post request and not the response from that post request.
Basically the flow of my app is. I insert an item into a DB. Using that insertId I then insert multiple more items into a database that are 'children' of the first insertId. But I also need to then send these values to an API. This API will then return me another ID which I will ultimately associate with my own previous insertID. The code is roughly as follows (I've removed some other logic and error handling for the sake of brevity).
let name = req.body.name;
let value = req.body.values;
let obj = {name:name};
let entries = [];
let a = db.items.insertItem(name);
let b = a.then((data) => {
let insertId = data.insertId;
let promises = [];
values.forEach((val) => {
entries.push({value:val)})
promises.push( db.values.insertValuesForItem(insertId,val));
})
obj.entries = entries;
return promises;
})
let c = b.then((data) => {
return request.post(constants.devUrl,
{
headers:{
'Authorization': 'bearer ' + constants.developerToken,
'content-type': 'application/json'
},
json:obj
});
});
Promise.all([a,b,c]).then(([resa,resb,resc]) => {
//resc here contains the post request info e.g the post data and headers
// what I really want from resc is the response from my post request
res.redirect(200,'/items/' + resa.insertId);
})
As I mentioned previously, in resc I actually need the response from the API request, not details of the request itself. Any ideas how I achieve that?