How can i return the result of an http request in a promise?

Hi guys,

can you please help me on this:

My get function should return the result of the http request or a Subscribale of it. How can i manage to do this?

I need the accesstoken in the http.get so i have to do the storage.get but i need to return the response of the http request.

public get(route: string = ‘’, params: string = ‘’){
this.storage.get(‘timeout_accesstoken’).then(data =>{
this.http.get( … )
});
}

Thank you. @rapropos

Try this:

return new Promise(resolve => {
  this.http.get('http://your.server.url)
    .map(res => res.json())
    .subscribe((data: any) => {
      resolve(data.Data);
    }, error => {
      resolve(error);
    });
});
5 Likes

Thanks for your reply.

I had that but my problem is, that since i want to do that in rc1 / rc 2 / r3 it cannot find “Promise”. In my other app which i built with the beta if ionic 2 it works.

Do you have any solution how i can fix it it says "Corresponding file not included in tsconfig.json.

Here my tsconfig.json:
{
“compilerOptions”: {
“allowSyntheticDefaultImports”: true,
“declaration”: false,
“emitDecoratorMetadata”: true,
“experimentalDecorators”: true,
“lib”: [
“dom”,
“es2015”
],
“module”: “es2015”,
“moduleResolution”: “node”,
“sourceMap”: true,
“target”: “es5”
},
“include”: [
“src/**/*.ts”
],
“exclude”: [
“node_modules”
],
“compileOnSave”: false,
“atom”: {
“rewriteTsconfig”: false
}
}

its will be build try to run it

1 Like

OMG just wasted about 4 hours.

Thank you so much.

My WebStorm told me that it is an error so i trusted him.

I owe you some beers :slight_smile:

I am wait for that beers :slight_smile:
Happy to halp.
don’t forget to like my answer.
Have a nice day/

Update the TypeScript version being used by your IDE to version 2.

I see this code a lot, and don’t like it very much. See this issue.

Thank you for that - updated the TypeScript Plugin - now working fine.

I also don’t like it but seems to be the only way working well - tried about 4 hours to figure out another way.

My be on error, you need to user reject method?:

indent preformatted text by 4 spaces
var self = this;
return new Promise(function(resolve, reject)  {
 self.http.get('http://your.server.url)
   .map(res => res.json())
   .subscribe((data: any) => {
     resolve(data.Data);
   }, error => {
     reject(error);
   });
 });

Observable can be easy converted to promise with toPromise method. Example

public signIn(): Promise<any> {
        return this.http.get('some_url').toPromise()
            .then(resp => resp.json());
    }
2 Likes

Thank you! Great. It helped me.

Thanks a lot man you are the best!