Skip to main content
added 818 characters in body
Source Link
bigblind
  • 12.9k
  • 14
  • 72
  • 132

currently, it's the callback that's returning the resource, which doesn't help you much. You can't delay a return statement until an async function has completed, but you can work with $q promise.

ngular.module('app').factory('newService', function($resource, configService, $q) {
    var resourcePromise = $q.defer();
    configService.get(function(config) {
        resourcePromise.resolve($resource(config.webServicePath + '/api/names', {},
            { 'update': { method:'PUT' } }
        ));
    });
    return resourcePromise.promise;
});

Here's how $q works

  1. You need to return a value that you get in the future, so you defer it using $q.defer()
  2. This returns a deferred object.
  3. You make your async call, and in its success callback, you call .resolve on the deferred object.
  4. If the callback fails, you call .rejecton the deferred object.
  5. You return deferredobject.promise, which is a promise object.

Using a service that returns promises

When you expect a value from a promise, you can listen for it by passing a callback to its thenmethod. If the promise might be rejected (the operation failed), it's a good idea to pass a second callback, which will be called when the promise is rejected.

Here's a simple example of a service that returns a promise, and how to use it:

http://jsfiddle.net/rP5Gh/

currently, it's the callback that's returning the resource, which doesn't help you much. You can't delay a return statement until an async function has completed, but you can work with $q promise.

ngular.module('app').factory('newService', function($resource, configService, $q) {
    var resourcePromise = $q.defer();
    configService.get(function(config) {
        resourcePromise.resolve($resource(config.webServicePath + '/api/names', {},
            { 'update': { method:'PUT' } }
        ));
    });
    return resourcePromise.promise;
});

currently, it's the callback that's returning the resource, which doesn't help you much. You can't delay a return statement until an async function has completed, but you can work with $q promise.

ngular.module('app').factory('newService', function($resource, configService, $q) {
    var resourcePromise = $q.defer();
    configService.get(function(config) {
        resourcePromise.resolve($resource(config.webServicePath + '/api/names', {},
            { 'update': { method:'PUT' } }
        ));
    });
    return resourcePromise.promise;
});

Here's how $q works

  1. You need to return a value that you get in the future, so you defer it using $q.defer()
  2. This returns a deferred object.
  3. You make your async call, and in its success callback, you call .resolve on the deferred object.
  4. If the callback fails, you call .rejecton the deferred object.
  5. You return deferredobject.promise, which is a promise object.

Using a service that returns promises

When you expect a value from a promise, you can listen for it by passing a callback to its thenmethod. If the promise might be rejected (the operation failed), it's a good idea to pass a second callback, which will be called when the promise is rejected.

Here's a simple example of a service that returns a promise, and how to use it:

http://jsfiddle.net/rP5Gh/

Source Link
bigblind
  • 12.9k
  • 14
  • 72
  • 132

currently, it's the callback that's returning the resource, which doesn't help you much. You can't delay a return statement until an async function has completed, but you can work with $q promise.

ngular.module('app').factory('newService', function($resource, configService, $q) {
    var resourcePromise = $q.defer();
    configService.get(function(config) {
        resourcePromise.resolve($resource(config.webServicePath + '/api/names', {},
            { 'update': { method:'PUT' } }
        ));
    });
    return resourcePromise.promise;
});