I have a function that contains a query function and combined callback in addition to its calling function:
function MyCallingFunction()
{
var response = MyFunction();
//...do some stuff with response
}
Which leads into:
function MyFunction()
{
//does some stuff
ReturnRows(query, connection, function(result)
{
//... Omitted error handling
if(result == null) //this is a pseudocode for clarity
{
return "There was an error!";
}
});
return "There were no errors!";
}
There are two ways that I've considered approaching. The first, and most obvious, is somehow allowing a "double return". By which I mean a return in the callback returns the housing (for want of a better word) function.
The second way involves something just like that:
function MyFunction()
{
//does some stuff
var returned = ReturnRows(query, connection, function(result)
{
//... Omitted error handling
if(result == null) //this is a pseudocode for clarity
{
return "Error";
}
else
{
return "Success";
}
});
if(returned.includes("Error"))
{
return "There was an error!";
}
else
{
return "There were no errors!";
}
}
Not only is this approach quite clumsy, I need to be absolutely sure that the callback function has finished executing (in this example, it won't have). Ordinarily I would put my code within the callback block to make sure it's executed, but when trying to return like this I obviously can't.
Is there an elegant solution for someone new to node?