title: Ionic 2 - Promise function (must be instanceof Promise error)
body: <p>Does anyone encounter this error before and could shed some light for me? I was stuck for days :(</p>
  
  <p><img src="https://i.stack.imgur.com/aD49e.png" alt="Promise Error"></p>
  
  <p>I'm trying to call my Web API function using a Promise function in my typescript file. Basically I am trying to pass a list of keys(int or string) to my Web API and i encountered the error there. Does anyone know how to solve this issue with 
  (must be instanceof Promise error) ?</p>
  
  <p><strong>Controller:</strong></p>
  
  <pre><code>import { Injectable } from 'angular2/core';
  import { Http, Headers, RequestOptions } from '@angular/http';
  import 'rxjs/add/operator/toPromise';
  import 'rxjs/add/operator/map';
  
  export class WorkOrderService {
      private tasks: any;
      private taskItems: any;
      private woSummary: any;
  
  
      static get parameters() {
          return [[Http]];
      }
  
      public constructor(private http: Http) {
      }
  
  public DownloadWO(ClientApiUrl, workOrders) {
  
      var headers = new Headers();
      headers.append('Content-Type', 'application/json');
  
      var workOrderKeys = new Array();
  
      for (let workOrder of workOrders) {
          if (workOrder.checked) {
              workOrderKeys.push(workOrder.WorkOrderKey);
          }
      }
  
      //ERROR CANT SOLVE
      return new Promise((resolve, reject) =&gt; {
          this.http.post(ClientApiUrl + "workorder/UpdateAndLockWorkOrders", workOrders, { headers: headers })
              .map(res =&gt; res.json())
              .subscribe(
              data =&gt; {
                  resolve(data);
              },
              err =&gt; {
                  reject(err);
              });
      });
  }
  
  }
  </code></pre>
  
  <p><strong>Behind-code .ts</strong></p>
  
  <pre><code>import { Component } from '@angular/core';
  import { AlertController, ModalController, NavController, LoadingController } from 'ionic-angular';
  
  import { Global } from '../../app/global';
  
  import { LocationService } from '../../services/locationService';
  import { DepartmentService } from '../../services/departmentService';
  import { WorkOrderService } from '../../services/workOrderService';
  
  import { HomePage } from '../home/home';
  import { ModalPmTaskPage } from './modal-pmTask'
  
  @Component({
      selector: 'page-downloadWO',
      templateUrl: 'downloadWO.html'
  })
  
  export class DownloadWOPage {
      private sites: any;
      private selectPLocID: string;
      private selectedSite: number;
      private parentLocations: any;
      private selectedPLocation: number;
      private locations: any;
      private selectedLocation: number;
      private departments: any;
      private selectedDepartment: number;
      private fromDate: Date;
      private toDate: Date;
      private workOrders: any;
      private isLock: number;
      private pmTaskItems: any;
  
      public constructor(
          private navCtrl: NavController,
          private workOrderService: WorkOrderService,
          private locationService: LocationService,
          private departmentService: DepartmentService,
          private global: Global,
          private alertCtrl: AlertController,
          private modalCtrl: ModalController,
          private loadingCtrl: LoadingController) {   
      }
  
  public download() {
          let loader = this.loadingCtrl.create({
              content: "Please wait..."
          });
  
          //Download &amp; Lock WorkOrders
          this.workOrderService.DownloadWO(this.global.getClientApiUrl(), this.workOrders).then(data =&gt; {
              loader.dismiss();
              let alert = this.alertCtrl.create({
                  title: 'Success',
                  subTitle: 'Work Orders has downloaded sucessfully',
                  buttons: [{
                      text: 'OK',
                      handler: () =&gt; {
                          this.navCtrl.push(HomePage);
                      }
                  }]
              });
              alert.present();
          }).catch(function (err) {
              loader.dismiss();
              let alert = this.alertCtrl.create({
                  title: 'Error',
                  subTitle: err,
                  buttons: [{
                      text: 'OK'
                  }]
              });
              alert.present();
          });
      }
  }
  </code></pre>
  
  <p><strong>HTML:</strong></p>
  
  <pre><code>&lt;ion-content&gt;
  ...
      &lt;ion-item-divider color="primary"&gt;Select Work Order&lt;/ion-item-divider&gt;
      &lt;ion-scroll scrollY="true" style="height:50vh;"&gt;
          &lt;ion-list&gt;
              &lt;ion-item text-wrap *ngFor="let workOrder of workOrders;"&gt;
                  &lt;ion-checkbox [(ngModel)]="workOrder.checked" &gt;{{workOrder.checked}}&lt;/ion-checkbox&gt;
                  &lt;ion-label&gt;
                      &lt;h2&gt;
                          &lt;b&gt;Work Order ID:&lt;/b&gt; {{workOrder.WorkOrderID}}
                      &lt;/h2&gt;
                      &lt;h2&gt;
                          &lt;b&gt;PM Task ID:&lt;/b&gt; {{workOrder.TaskId}}
                      &lt;/h2&gt;
                      &lt;h2&gt;
                          &lt;b&gt;Asset ID:&lt;/b&gt; {{workOrder.AssetID}}
                      &lt;/h2&gt;
                      &lt;button ion-button color="secondary" (click)="viewTaskItemFromServer(workOrder.WorkOrderKey)"&gt;View PM Task Items&lt;/button&gt;
                  &lt;/ion-label&gt;
              &lt;/ion-item&gt;
          &lt;/ion-list&gt;
      &lt;/ion-scroll&gt;
      &lt;button ion-button block (click)="download()"&gt;Download&lt;/button&gt;
  &lt;/ion-content&gt;
  </code></pre>
  
  <p>C# Web API:</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Configuration;
  using System.Data;
  using System.Data.SqlClient;
  using System.Net;
  using System.Net.Http;
  using System.Web.Http;
  using MaxPro_WebService.Interface.Controllers;
  using MaxPro_WebService.Interface.Models;
  using System.Net.Http.Headers;
  using System.Web.Script.Serialization;
  using CMMS.AssetsMaintMgmt_BLLAx;
  using System.Transactions;
  using System.IO;
  using System.Threading.Tasks;
  using Newtonsoft.Json.Linq;
  
  namespace MaxPro_Client_WebService.Controllers
  {
      [RoutePrefix("api/WorkOrder")]
      public class WorkOrderController : ApiController, IWorkOrderController
      {
          [Route("UpdateAndLockWorkOrders")]
          [HttpPost]
          public async Task&lt;HttpResponseMessage&gt; UpdateAndLockWorkOrders([FromBody]List&lt;string&gt; workOrderKeys)
          {
              string errorMsg, exMsg;
              errorMsg = exMsg = string.Empty;
              DateTime dtNow = DateTime.Now;
  
              errorMsg = exMsg = string.Empty;
  
              try
              {
  
                  using (var client = new HttpClient())
                  {
                      ////IL: TRY TO PASS IN CLASS
                      //client.BaseAddress = new Uri("http://localhost/MaxPro_WebService/");
                      //client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
                      //var response = await client.PostAsJsonAsync("api/WorkOrder/uploadWorkOrders", workOrders);
                      IEnumerable&lt;WorkOrder&gt; workorders = null;
                      client.BaseAddress = new Uri(ConfigurationManager.AppSettings["ServerBaseURLAddress"]); //Question how to get URL of web service
                      client.DefaultRequestHeaders.Accept.Clear();
                      client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
  
                      var response = await client.PostAsJsonAsync("api/WorkOrder/GetWorkOrders", workOrderKeys);
  
                      if (response.IsSuccessStatusCode)
                      {
                          string responseString = response.Content.ReadAsStringAsync().Result;
                          //JSON Object TO Work Order class
                          workorders = response.Content.ReadAsAsync&lt;IEnumerable&lt;WorkOrder&gt;&gt;().Result;
                          if (InsertRecords(workorders, out errorMsg))
                          {
                              //IL: 3/2/2017 working for LockWorkOrder
                              //client.BaseAddress = new Uri(ConfigurationManager.AppSettings["ServerBaseURLAddress"]); //Question how to get URL of web service
                              client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
                              response = await client.PostAsJsonAsync("api/WorkOrder/LockWorkOrders", workOrderKeys);
  
                              if (response.IsSuccessStatusCode)
                              {
                                  //LOCKING SUCCESSFUL
                                  return Request.CreateResponse(HttpStatusCode.OK);
                              }
                              else
                              {
                                  //LOCKING UNSUCCESSFUL
                                  return Request.CreateErrorResponse((HttpStatusCode)422, new HttpError(exMsg));
                                  //Something has gone wrong, handle it here
                              }
                          }
                          else
                          {
                              //SAVE WO UNSUCCESSFUL
                              return Request.CreateErrorResponse((HttpStatusCode)422, new HttpError(exMsg));
                              //Something has gone wrong, handle it here
                          }
                      }
                      else
                      {
                          //Something has gone wrong, handle it here
                          return Request.CreateErrorResponse((HttpStatusCode)422, new HttpError(exMsg));
                      } 
                  }
              }
              catch (TransactionAbortedException ex)
              {
                  exMsg = ex.Message;
                  return Request.CreateErrorResponse((HttpStatusCode)422, new HttpError(exMsg));
              }
              catch (ApplicationException ex)
              {
                  exMsg = ex.Message;
                  return Request.CreateErrorResponse((HttpStatusCode)422, new HttpError(exMsg));
              }
              catch (Exception ex)
              {
                  exMsg = ex.Message;
                  return Request.CreateErrorResponse((HttpStatusCode)422, new HttpError(exMsg));
              }
          }
      }
  }
  </code></pre>
  
  <p><strong>Enviroments:</strong></p>
  
  <p>IDE: VS2015
  Cordova CLI: 6.5.0</p>
  
  <p>Ionic CLI Version: 2.2.1</p>
  
  <p>Ionic App Lib Version: 2.2.0</p>
  
  <p>OS: Windows 7</p>
  
  <p>Node Version: v7.6.0</p>
  
  <p>Xcode version: not installed</p>