Can someone help me figure out how to access previous results in promises? I've been following the guidance on this stackoverflow thread.
I had success with 2 promises, but things got tricky when I added a 3rd promise that makes a POST request to an API. The result ended up being the post request itself instead of the response from the request.
My app's workflow involves inserting an item into a DB first. Then, using the insertId, I have to add multiple 'children' items into the database. Additionally, these values must be sent to an API which will return another ID to associate with the initial insertId. Here is a simplified version of my code:
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);
})
However, I need the actual response from the API request in resc, not just details about the request itself. Any suggestions on how to achieve this?