Before you all start screaming about refactoring and callbacks, read the question fully, please :)
I have the following situation in javascript (code simplified for convenience):
function myFunc(){
var myItems = getItemsAsync(); //this returns a promise
return {
settings: {
type: "items",
component: "accordion",
items: myItems //WRONG! This is a promise, not the data itself!
}
}
}
Here's the situation: I have an external framework that calls myFunc
and expects the returned object {settings: etc...}
to have all values already filled in, including the items
sub-object. I have no control over how this framework calls myFunc
, so I cannot change it to accept a promise or partial result, the returned object must be "ready" when myFunc returns.
Now, as you can see, here's the problem: to obtain the items
I need to call an API that is asynchronous by design, and returns a promise. Unfortunately, this API is also out of my control, so I cannot modify it to work synchronously.
So basically I am at a stalemate: I have to synchronously return a complete object, but one of the pieces of that object can only be obtained asynchronously!!
Is there a way out of this? All the similar questions I've seen here on SO suggest that waiting for a promise to be complete cannot be done... is it true?
I know what you're all dying to say: "CHANGE EITHER THE CALLER OR THE API", but unfortunately they are both components that are totally out of my control, as they're part of existing systems that i CANNOT modify (due to bureaucratic, rather than technical, reasons)
Any ideas to get out of this?