Http provider Observable.toPromise () does not work as expected in promise chains

If I call the method that supports the promise, using the support of the ng2 http Observable.toPromise () provider, it works as expected, but when I use it as part of the promise chain, it solves the returned promise before processing then the handler and returns the result.

Any known issues getting Observable.toPromise () to work in promise chains or alternative ways I can check to make it compatible with the promise of the result? I was blocked by this allow promise before the http request, the last element in the promise chain, completed the asynchronous request request and returned the result.

for example

this.myService.getSomethingInvolvingingMultiplePromiseCalls().then(result => {
    let valueFromSomethingInvolvingMultiplePromiseCalls = result;
}, err => { 
    console.error('landed in app.component outer promise rejected handler, see output window for details')
})

public getSomethingInvolvingingMultiplePromiseCalls(): Promise<string> {
    return this.getSomethingInvolvingPromiseCall().then(resultPromise1 => {
        let resultPromise1propertyFoo = resultPromise1.propertyFoo;
            return this.getSomethingInvolvingNg2HttpProviderToPromiseCall(resultPromise1propertyFoo);
        }
        .then(resultPromise2 => {
            let resultPromise2propertyBar = resultPromise2.propertyBar;
            return resultPromise2propertyBar;
        }   
    }

getSomethingInvolvingNg2HttpProviderToPromiseCall(arg1: string): Promise<string> {
   let body = 'some body content leveraging arg1';
   let headers = new Headers({ 'Authorization': 'Bearer ' + accessToken, 'Content-Type': 'application/x-www-form-urlencoded' });
   let options = new RequestOptions({ headers: headers });

   return this.http.post(resourceBaseAddress + '/someRestApi', body, options).toPromise().then(response => {
        let responseJson = response.json();
        return responseJson['someJsonProperty'];
      });
    }
}

in advance in advance for any ideas or suggestions.

+3
2

.

typescript, , , , angular2 http provider toPromise().

-, , - , http- toPromise() .

public getSomethingInvolvingingMultiplePromiseCalls(): Promise<string> {
    let resolveFn, rejectFn;
    let promise = new Promise((resolve, reject) => { resolveFn = resolve; rejectFn = reject; });

    this.getSomethingInvolvingPromiseCall().then(resultPromise1 => {
        this.getSomethingInvolvingNg2HttpProviderToPromiseCall(resultPromise1).then(resultPromise2 => resolveFn(resultPromise2));
    }

    return promise;  // return the promise for outside callers to wait on
}
+3

. .

var promise = new Promise((resolve, reject) => {
  resolve(3)
}).then((num) => {
  return Rx.Observable.create((observer) => {
    setTimeout(() => {
      observer.next(5);
      observer.onCompleted();
    }, 0)
  }).toPromise()
}).then((num) => {
  return num * 2;
})

promise.then((number) => {
  alert(number);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/rxjs/4.0.7/rx.all.js"></script>
Hide result

.toPromise(), observer.onCompleted(). , , .

+1

All Articles