Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular Service Promises

So, I've got an Angular app that makes restful calls to the server. There is a service that wraps up the calls to the server. I currently have a method on the service that simply returns the promise from the $http service. I'd like to add some additional processing on that method call, but I'm not sure how to do it because of the asynchronous nature of the promise.

Currently in typescript:

class BoardService {
    private $http;

    constructor($rootScope: IRootScope, $http: ng.IHttpService) {
          this.$http = $http;
    }

    fetchBoard(id: number) {
        return this.$http.get("/api/board/" + id);
    }
}

I'd like to get it to something like this:

fetchBoard2(id: number) {
    this.$http.get("/api/board/" + id).success(function(data)
    {
        // Manipulate the data

    });

    // return manipulated data; 
}

How would you do this?

like image 557
Darthg8r Avatar asked May 29 '15 15:05

Darthg8r


People also ask

Which is better promise or Observable?

the Promise is always asynchronous, while the Observable can be either asynchronous or synchronous, the Promise can provide a single value, whereas the Observable is a stream of values (from 0 to multiple values), you can apply RxJS operators to the Observable to get a new tailored stream.

What is difference between promise and Observable in angular?

Promises deal with one asynchronous event at a time, while observables handle a sequence of asynchronous events over a period of time. Emit multiple values over a period of time. Emit a single value at a time. Are lazy: they're not executed until we subscribe to them using the subscribe() method.

What are Promises angular 9?

Because they are promises you need to respect their resolve time, it means to wait until they are completed and that means you can't call them as a normal function when you rely on their result. Also because it's an angular app try to convert them to rxjs stream via from function.

What is resolve in promise angular?

resolve() The Promise. resolve() method "resolves" a given value to a Promise . If the value is a promise, that promise is returned; if the value is a thenable, Promise. resolve() will call the then() method with two callbacks it prepared; otherwise the returned promise will be fulfilled with the value.


1 Answers

Tricky sentence warning! Because promises are asynchronous, anything returning data based on data from a promise must itself return a promise. You want fetchBoard2 to return a promise that gets resolved once the $http promise has come back and you've manipulated the data. You do this with Angular's $q service.

fetchBoard2(id: number) {
  var deferred = $q.defer();

  $http.get("/api/board/" + id).success(function(data) {
    var newData = doSomething(data);
    deferred.resolve(newData);
  });

  return deferred.promise;
}

Managing extra deferred objects gets quickly fiddly, so you can use then to insert your own manipulation into the pipeline.

fetchBoard3(id: number) {
  return $http.get(...).then(function(data) {
    return doSomething(data);
  });
}

For more detail, here's a good article.

like image 50
Kristján Avatar answered Sep 21 '22 14:09

Kristján