Delaying function return or another way to wait for network data

Hi,

I have a problem with the event driven nature of Corona. 

I have a “class” which safes and calculates data for a fleet. One of the data is the target planet. This planet is available through a function called getPlanet which returns the planet-data.

But here comes the problem. If the planet-data isn’t loaded, a network request is required do load this data. This happens asynchronus, so the data isn’t available for some time. Is there a way to delay the return of the function till the data is loaded?

I want to handle the problem that the data is not loaded inside the function, not at every place i call the function.

Any idea how i can do this?

function Fleet:getTargetPlanet() if self.targetPlanet == nil then -- this loads the data from the backend and replaces self.targetPlanet -- it calls callback, when the data is available (I'm searching for a suitable callback function) self:refreshData(self.targetPlanetId, callback) -- this is wrong. I want a way to return self.targetPlanet after it has been loaded return false end return self.targetPlanet end local myplanet = self:getTargetPlanet() -- I don't want to do the error handling here, but in the function getTargetPlanet

Use custom events for scenarios like this, really handy.

Use custom events for scenarios like this, really handy.