It is very difficult to talk about what you are doing here, without a concrete, real example. This is definitely not a template that I often use in my code, so I have a suspicion that you can fix it by overestimating your assumptions.
By saying, you can use the promise chain. Note that in this case, promThatLoadsVar is a promise, it is not a function that returns a promise. This means that it maintains state, and the second time this code runs forward, it immediately returns.
promisethatLoadsVar.then(function(value) { myvar = value; doSomeTreatment(myvar); })
If you could give a clearer question, I would be happy to provide you with a clearer answer.
Change I want to elaborate on the example presented in the comments. Here is a solution using the LoDash library. (BTW, you should use lodash or underscore, they are basically the standard javascript library).
var getData = _.once(function() { return $.getJSON(...) }); $('button').on('click', function() { getData().then(function(data) { showDialog(data) }) })
Note that getData is a wrapped function that, after the first call, will return the same thing over and over without calling the function again. On the first call, it returns a promise that is resolved when the data is retrieved. The second time you return the same promises that are likely to be resolved.
Do you want to pass getData parameters?
var getData = _.memoize(function(id) { return $.getJSON(url+id) }); $('button').on('click', function() { getData($('#selector').val()).then(function(data) { showDialog(data) }) })
This will do the same, but cache promises with the first input parameter passed to getData .