I am new to Promises, and thought I have more or less understood the topic for now, but apparently not very well.
I am asking for an explanation of what is happening in the following, I think I have an understanding of what is happening, but not why...
My misunderstanding is in this piece of code:
(async () => {
try{
const result = await neoDB.getPatientById(requestedPatientID)
.then (result => {
console.log('2----------------'); //VIEW CONSOLE OUTPUT
console.log(result); //<---WHY RESULT IS UNDEFINED?? VIEW CONSOLE OUTPUT
resolve('Found Patient ' + requestedPatientID);
res.send(result); //sending patient's data as a response. If no patient was found, TBD is sent
});
}
catch (error) {
console.log('-- in catch');
console.log(error);
}
})();
and neoDB.getPatientById(id):
async function getPatientById(id){
const session = driver.session();
try {
const result = await session.run('MATCH (p:Patient {id : ' + id + '}) RETURN p')
.then(result => {
const PatientJson = convertDBPatientObjToJson(result.records[0]._fields[0].properties)
console.log(PatientJson); //VIEW CONSOLE OUTPUT
console.log('1----------------'); //VIEW CONSOLE OUTPUT
return PatientJson;
});
}
catch (error){
console.log('-- in catch');
console.log(error);
}
finally {
await session.close()
}
}
I understand that a promise is returned as resolved if there is a return, but why doesn't the code in the .then method (in the first piece of code) waits for the returned value?
Console output:
node localDB.js
localDB-MS started on port: 3002
{id: 1, phone_number: 11111, a: patient1, b: 25, c: true}
1----------------
2----------------
undefined
As you can see the output timeline corresponds to the logic, but something is going wrong in the content of result.
Thanks for any help, I am trying to understand what's going on here, so please add a word or two :)