Skip to main content
added 5 characters in body; edited title
Source Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238

Using the $q service in AngularJS to handle promises. Am I doing it right?

I have a service which needs to store some settings data in a DB and at the same time maintain a reference to that in the user's localStorage (we want two users logged in to the same account to be able to view them differently). This method looks for an existing previewSession in the localStorage and if not found, creates one and generates a unique URL.

As you can see at the beginning, I'm using the $q service to return a promise and then resolving that promise as and when all operations have finished. There is one use of the $http service and the save call is on a $resource object.

Is this a proper way to handle such a use-case, or could I have done it better?

To summarize the requirements:

  1. Method gets called with a settings parameter
  2. Method must always return a URL (unless an error occurs on the db level or some such thing)

So, essentially, I have a service which needs to store some settings data in a DB and at the same time maintain a reference to that in the user's localStorage (we want two users logged in to the same account to be able to view them differently). So this method looks for an existing previewSession in the localStorage and if not found, creates one and generates a unique url.

As you can see at the beginning I'm using the $q service to return a promise and then resolving that promise as and when all operations have finished. There is one use of the $http service and the save call is on a $resource object.

So is the a proper way to handle such a use-case? Or could I have done it better.

So to summarize the requirements:

  1. Method gets called with a settings paramter
  2. Method must always return a url (unless an error occurs on the db level or some such thing)

Using the $q service in AngularJS to handle promises. Am I doing it right?

So, essentially, I have a service which needs to store some settings data in a DB and at the same time maintain a reference to that in the user's localStorage (we want two users logged in to the same account to be able to view them differently). So this method looks for an existing previewSession in the localStorage and if not found, creates one and generates a unique url.

As you can see at the beginning I'm using the $q service to return a promise and then resolving that promise as and when all operations have finished. There is one use of the $http service and the save call is on a $resource object.

So is the a proper way to handle such a use-case? Or could I have done it better.

So to summarize the requirements:

  1. Method gets called with a settings paramter
  2. Method must always return a url (unless an error occurs on the db level or some such thing)

Using the $q service in AngularJS to handle promises

I have a service which needs to store some settings data in a DB and at the same time maintain a reference to that in the user's localStorage (we want two users logged in to the same account to be able to view them differently). This method looks for an existing previewSession in the localStorage and if not found, creates one and generates a unique URL.

As you can see at the beginning, I'm using the $q service to return a promise and then resolving that promise as and when all operations have finished. There is one use of the $http service and the save call is on a $resource object.

Is this a proper way to handle such a use-case, or could I have done it better?

To summarize the requirements:

  1. Method gets called with a settings parameter
  2. Method must always return a URL (unless an error occurs on the db level or some such thing)
Source Link

Using the $q service in AngularJS to handle promises. Am I doing it right?

var PREVIEW_SESSION_KEY_BASE = '<a constant string>';    
function getPreviewSessionUrl(settings) {
        var deferred = $q.defer();

        $http.get('/' + appCode + '/reservedDomain',
            function(appResponse) {
                $localForage
                    .getItem(PREVIEW_SESSION_KEY_BASE + themeId)
                    .then(function(data) {
                        if (data) {
                            deferred.resolve(appResponse + '?<a query key>=' + data);
                        } else {
                            previewSessionResource.save(
                                {},
                                {
                                    settings: settings
                                },
                                function(previewResponse) {
                                    $localForage
                                        .setItem(PREVIEW_SESSION_KEY_BASE + themeId, previewResponse)
                                        .then(function(data) {
                                            deferred.resolve(appResponse + '?<a query key>=' + data);
                                        });
                                },
                                function(error) {
                                    deferred.reject(error);
                                }
                            );
                        }
                    });
            },
            function(error) {
                deferred.reject(error);
            }
        );

        return deferred.promise;
    }

So, essentially, I have a service which needs to store some settings data in a DB and at the same time maintain a reference to that in the user's localStorage (we want two users logged in to the same account to be able to view them differently). So this method looks for an existing previewSession in the localStorage and if not found, creates one and generates a unique url.

As you can see at the beginning I'm using the $q service to return a promise and then resolving that promise as and when all operations have finished. There is one use of the $http service and the save call is on a $resource object.

So is the a proper way to handle such a use-case? Or could I have done it better.

So to summarize the requirements:

  1. Method gets called with a settings paramter
  2. Method must always return a url (unless an error occurs on the db level or some such thing)