lately I've had some trouble dealing with asynchronous methods. I've been getting around using setTimeout(); but I know this is neither optimal nor recommended. Any help is greatly appreciated.
I have an asynchronous method inside a function and I want the function to wait until the async method is done to return the value. I have something like this:
function myFun{
var myarray = [];
db.asyncmethod(function (){
myarray.push("Something");
});
return myarray;
}
The problem is that myarray
is returned empty because asyncmethod
hasn't finished yet. How should I handle this?
Thanks =)