Returning promise problem


#1

I got a provider “fetchdata” to return promise

   getPostDetail(id)
  {

    return new Promise(function(resolve, reject)
    {

    let link = 'https://themooncreation.com/apps/data2.php?action=get_posts&id='+id;
    console.log(link);

      this.http.get(link).map(res => res.json()).subscribe(data =>
      {
  //        console.log("right here "+postDetail['USERNAME']);
          resolve(data);
          reject(console.log('some fail stuff'));
      });
    });

  }

and i am calling the provider by

this.fetchdata.getPostDetail(this.id).then(post => this.loadMap(post['LAT'], post['LON']));

but seems loadMap is not being called, anyone know why?


#2

Can you try doing this instead:

getPostDetail(id: string): Promise<Whatever> {
  return this.http.get(link)
    .map(rsp => rsp.json())
    .toPromise();
}

#3

it’s working now, thanks. but do you know why my version doesn’t work?


#4

Perhaps because of lost execution context, but explicit Promise construction should be avoided.