Skip to content

Requests Sharing deferred object #9

@mbowen000

Description

@mbowen000

I've noticed that sometimes when I am performing some action when a promise comes back from some kind of request - it is possible that the request will hand back a promise that does not belong to the current request (the one I expect will be evaluated). For example:

return force.query('SELECT Id, FROM PricebookEntry').then(function(response) {
    pricebookentries = response.records;
});

return force.query('SELECT Id, FROM Account').then(function(response) {
    accounts = response.records;
});

If I perform these two queries in parallel - I have seen times where the response callback for the Account query will return the results from the PricebookEntry query. It's like the wires are getting crossed. I got around the problem by doing each successive call in a .then() block but that doesn't seem ideal.

Looking at the forceng source - I see this in the code:

 function request(obj) {

            if (!oauth || (!oauth.access_token && !oauth.refresh_token)) {
                deferred.reject('No access token. Login and try again.');
                return;
            }

            var method = obj.method || 'GET',
                headers = {},
                url = getRequestBaseURL();
            deferred = $q.defer();

Notice that the deferred is defined as a window variable (not var). So the deferred is actually shared across requests, which is interesting - and looks to be by design. Can you explain if this is the case and it is indeed supposed to operate like that? And if so, how to best perform multiple asynchronous requests without worrying about getting information for another request during promise resolve?

Thanks!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions