Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>To create a function that returns a promise is easy: </p> <pre><code>function test() { var defer = Q.defer(); // an example of an async call serverCall(function (request) { if (request.status === 200) { defer.resolve(request.responseText); } else { defer.reject(new Error("Status code was " + request.status)); } }); return defer.promise; } </code></pre> <p>Now you could use it with the .then().</p> <p>Here is an example of how could work your primeData function:</p> <pre><code>function primeData(forceRefresh) { function success(data) { if (!initialized) { datacontext.lookups = data.results[0]; initialized = true; } else { datacontext.lookups = { teams: getLocal('Teams', 'id'), // other lookup values here }; } } var getLookupsPromise = getLookups(forceRefresh); // .then method returns a new promise. var successPromise = getLookupsPromise.then(success); // success is called when the getLookupsPromise promise is resolved // Now success promise will resove when succes code is resoved. // If success where assync and it returned an other promise this will resolve when the promise resolves. var modelInitializePromise = getLookupsPromise.then(function () { model.initialize(datacontext); }); var datasetOnePromise = modelInitializePromise.then(getDatasetOne); var datasetTwoPromise = modelInitializePromise.then(getDatasetTwo); var nonDependantPromise = getNonDependantDataset(); // Generate a promise that will resolve when all promises are resolved return q.all([getLookupsPromise, successPromise, modelInitializePromise, datasetOnePromise, datasetTwoPromise, nonDependantPromise]); } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload