Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Return Observable inside the Promise

Could you tell me how can I return Observable here? I have tried many ways. But no success yet.

[ts] A function whose declared type is neither 'void' nor 'any' must return a value.

  deleteInvite(inviteId):Observable<HttpResponse<Object>> {
    this.getHttpHeader().then(headers => {
      return this.http.post(this.apiURL + "/deleteInvite", inviteId, { observe: "response", headers: headers })
    }).catch(err => {
      console.error(err);
    });
  }

  async getHttpHeader() {
    const accessToken = await this.getJWTToken();
    let headers = new HttpHeaders().set('Authorization', `Bearer ${accessToken}`);
    return headers;
  }
like image 761
Sampath Avatar asked Jun 15 '18 10:06

Sampath


1 Answers

FOR rxjs version: 6.0.0 onwards

import { from } from 'rxjs';

 deleteInvite(inviteId):Observable<HttpResponse<Object>> {
    return from(this.getHttpHeader()).pipe(
        switchMap(headers => {
            return this.http.post(this.apiURL + "/deleteInvite", inviteId, { observe: "response", headers: headers })
        }),
        catchError(err => {
            console.error(err);
        })
    );
}

For older versions of rxjs:

Convert the thisGetHeaders() to an Observable by using fromPromise

import { fromPromise } from 'rxjs/observable/fromPromise';


deleteInvite(inviteId):Observable<HttpResponse<Object>> {
    return fromPromise(this.getHttpHeader()).pipe(
        switchMap(headers => {
            return this.http.post(this.apiURL + "/deleteInvite", inviteId, { observe: "response", headers: headers })
        }),
        catchError(err => {
            console.error(err);
        })
    );
}
like image 59
Suraj Rao Avatar answered Oct 27 '22 11:10

Suraj Rao