Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Typescript 顺序连锁承诺_Typescript_Promise - Fatal编程技术网

Typescript 顺序连锁承诺

Typescript 顺序连锁承诺,typescript,promise,Typescript,Promise,我知道这个问题已经研究过一千次了,但我尝试了很多解决方案,但仍然不起作用 我想从http get链接承诺,但在运行下一个之前需要等待上一个,因为get方法返回一个在数组中推送的数字,并且需要正确排序 我尝试过的是:将每个承诺都放在一个数组中,然后使用reduce方法,但它不起作用。 我认为棘手的是,我有一个get参数的条件,它的值会发生变化。 这些值在一个数组中,所以我尝试使用wait执行forEach,但没有成功 这是我目前的代码: async getAEObservable(checkIn:

我知道这个问题已经研究过一千次了,但我尝试了很多解决方案,但仍然不起作用

我想从http get链接承诺,但在运行下一个之前需要等待上一个,因为get方法返回一个在数组中推送的数字,并且需要正确排序

我尝试过的是:将每个承诺都放在一个数组中,然后使用reduce方法,但它不起作用。 我认为棘手的是,我有一个get参数的条件,它的值会发生变化。 这些值在一个数组中,所以我尝试使用wait执行forEach,但没有成功

这是我目前的代码:

async getAEObservable(checkIn: Date, checkOut: Date, adults: number, HotelID) {
    var In = checkIn;
    var Out =checkOut;


    var  promises=[];

    var id = []
    HotelID.forEach(element =>
      id.push(element))

    var index=0
    var ind = HotelID[0]
    if (HotelID.length !== 0) {
    if (ind !== "NULLNULL") {
      console.log('ok')
      const httpOptions = {
        headers: new HttpHeaders({
          'Content-Type': 'application/json',
          'Ama-Pos': '{"agentSine": "A9998WS","agentUserID": "AA","agentDutyCode": "GS","agencyDetails":{"currencyCode": "EUR","identifiers": [{"type": "IATA","id": "QIJI12151"}],"address":{"cityCode": "NCE","countryCode": "FR"}}}'
        })
      };

        let checkIn_param = this.transformDate(checkIn);
        let checkOut_param = this.transformDate(checkOut);
        let apiUrl = `${this.apiRoot_v0}?hotelIds=${ind.toString()}&checkInDate=${checkIn_param}&checkOutDate=${checkOut_param}&roomQuantity=1&adults=2&radius=5&radiusUnit=KM&rateCodes=RAC&paymentPolicy=NONE&includeClosed=true&bestRateOnly=true&view=FULL&sort=NONE`
        let url = `${apiUrl}`;
        console.log(url)
        return await this.apiCall(url, httpOptions, checkIn, checkOut, adults, HotelID); // here we are calling a function which contain a api call inside this function we are resolving the promise
      } else {

        // here we are not creating a promise bcs here we have simple push operation

        this.resAE.price.push(0)
        this.getAEObservable(checkIn, checkOut, adults, HotelID.slice(1));
        return this.resAE
      }
    }
    else {return this.resAE}
      }










async apiCall(url, httpOptions,checkIn, checkOut, adults, HotelID) {
   await new Promise(async (resolve, reject) => {
    await this.httpClient.get<any>(url, httpOptions).toPromise().then(result => {
      console.log(result)
      var price = result['data']['0']['offers']['0']['price']['total'];

      console.log(price)

      this.resAE.price.push(price);
      console.log(this.resAE)


  return this.resAE
    }).then(async resp => { if (HotelID.length !=0){
       this.getAEObservable(checkIn, checkOut, adults, HotelID.slice(1));}
      else{
      resolve();}
    }).catch(this.handleError)

  })

}






async apiCall(url, httpOptions,checkIn, checkOut, adults, HotelID) {
  return await new Promise(async (resolve, reject) => {
    this.httpClient.get<any>(url, httpOptions).toPromise().then(result => {
      console.log(result)
      var price = result['data']['0']['offers']['0']['price']['total'];

      console.log(price)

      this.resAE.price.push(price);
      console.log(this.resAE)

      return this.resAE;
    }).then(resp => { if (HotelID.length ===0){

      return this.resAE

    } else {
      this.getAEObservable(checkIn, checkOut, adults, HotelID.slice(1));
      resolve()};
    }).catch(this.handleError)
  })
}
async getAEObservable(签入:日期,签出:日期,成人:号码,HotelID){
var-In=签入;
var Out=签出;
var承诺=[];
变量id=[]
HotelID.forEach(元素=>
id.push(元素))
var指数=0
var ind=HotelID[0]
如果(HotelID.length!==0){
如果(ind!=“NULLNULL”){
console.log('ok')
常量httpOptions={
标题:新的HttpHeaders({
“内容类型”:“应用程序/json”,
“Ama Pos”:“{”agentSine:“A9998WS”,“agentUserID:“AA”,“agentDutyCode:“GS”,“AgentDetails:{”currencyCode:“EUR”,“identifiers:[{”type:“IATA”,“id:“QIJI12151”}],“address:{”cityCode:“NCE”,“countryCode:“FR”}”
})
};
让checkIn_param=this.transformDate(checkIn);
让checkOut_param=this.transformDate(checkOut);
让apiUrl=`${this.apiRoot\u v0}?hotelIds=${ind.toString()}&checkInDate=${checkIn\u param}&checkOutDate=${checkOut\u param}&roomQuantity=1&dactors=2&radius=5&radiusnit=KM&rateCodes=RAC&paymentPolicy=NONE&includeClosed=true&bestrate=true&view=FULL&sort=NONE`
设url=`${APIRL}`;
console.log(url)
return wait this.apiCall(url、httpOptions、checkIn、checkOut、成人、HotelID);//这里我们调用一个函数,该函数包含一个api调用,我们正在解析这个承诺
}否则{
//这里我们不是在创造一个承诺bcs这里我们有简单的推送操作
此.rese.price.push(0)
这个.getAEObservable(签入、签出、成人、HotelID.slice(1));
把这个还给我
}
}
否则{返回此.rese}
}
异步apiCall(url、httpOptions、签入、签出、成人、HotelID){
等待新承诺(异步(解析、拒绝)=>{
等待这个.httpClient.get(url,httpOptions.toPromise()。然后(结果=>{
console.log(结果)
var price=result['data']['0']['offers']['0']['price']['total'];
控制台日志(价格)
这个。转售。价格。推送(价格);
console.log(this.rese)
把这个还给我
}).then(异步响应=>{if(HotelID.length!=0){
this.getAEObservable(签入、签出、成人、HotelID.slice(1));}
否则{
解析();}
}).接住(这个.把手错误)
})
}
异步apiCall(url、httpOptions、签入、签出、成人、HotelID){
返回等待新承诺(异步(解析、拒绝)=>{
this.httpClient.get(url,httpOptions.toPromise()。然后(结果=>{
console.log(结果)
var price=result['data']['0']['offers']['0']['price']['total'];
控制台日志(价格)
这个。转售。价格。推送(价格);
console.log(this.rese)
把这个还给我;
}).then(resp=>{if(HotelID.length==0){
把这个还给我
}否则{
这个.getAEObservable(签入、签出、成人、HotelID.slice(1));
解析()};
}).接住(这个.把手错误)
})
}
IDE将执行第一个get,填充我的数组,完成后,通过调用函数本身,但通过删除HotelID的第一个元素(我用于上一个get),继续执行下一个get调用

数组已填充,因此没有其他问题,这不是很好,我认为

我只是编辑我的问题,说console.log(url)显示url是按良好顺序创建的

您可以这样尝试

async getAEObservable(checkIn: Date, checkOut: Date, adults: number, HotelID) {
 console.log(HotelID);
    var In = checkIn;
    var Out =checkOut;

    var  promises=[];

    var id = []
    HotelID.forEach(element =>
    id.push(element))
    console.log(id)
    var index=0
    var ind = HotelID[0]
      if (ind !== "NULLNULL") {
        console.log('ok')
        const httpOptions = {
          headers: new HttpHeaders({
            'Content-Type': 'application/json',
            'Ama-Pos': '{"agentSine": "A9998WS","agentUserID": "AA","agentDutyCode": "GS","agencyDetails":{"currencyCode": "EUR","identifiers": [{"type": "IATA","id": "QIJI12151"}],"address":{"cityCode": "NCE","countryCode": "FR"}}}'
          })
        };

        let checkIn_param = this.transformDate(checkIn);
        let checkOut_param = this.transformDate(checkOut);
        let apiUrl = `${this.apiRoot_v0}?hotelIds=${ind.toString()}&checkInDate=${checkIn_param}&checkOutDate=${checkOut_param}&roomQuantity=1&adults=2&radius=5&radiusUnit=KM&rateCodes=RAC&paymentPolicy=NONE&includeClosed=true&bestRateOnly=true&view=FULL&sort=NONE`
        let url = `${apiUrl}`;
        console.log(url)
        return await apiCall(url, httpOptions); // here we are calling a function which contain a api call inside this function we are resolving the promise
      } else {

        // here we are not creating a promise bcs here we have simple push operation

        this.resAE.price.push(0)
        this.getAEObservable(checkIn, checkOut, adults, HotelID.slice(1));
        return this.resAE
      }

下面是api调用函数

apiCall(url, httpOptions) {
 return new Promise((resolve, reject) => {
this.httpClient.get<any>(url, httpOptions).toPromise().then(res => {
            console.log(res)
            var price = res.data[0].offers[0].price.total;
            console.log(price)
            this.resAE.price.push( price);
            console.log(this.resAE)
            console.log(id)
            console.log(id.slice(1))
            return this.resAE;
          }).then( resp => {
             this.getAEObservable(checkIn, checkOut, adults, HotelID.slice(1))
             resolve();
         }).catch(this.handleError)
})
}


apiCall(url,httpOptions){
返回新承诺((解决、拒绝)=>{
this.httpClient.get(url,httpOptions.toPromise()。然后(res=>{
console.log(res)
var price=res.data[0]。报价[0]。price.total;
控制台日志(价格)
这个。转售。价格。推送(价格);
console.log(this.rese)
控制台日志(id)
console.log(id.slice(1))
把这个还给我;
})。然后(resp=>{
this.getAEObservable(签入、签出、成人、HotelID.slice(1))
解决();
}).接住(这个.把手错误)
})
}
您可以这样尝试

async getAEObservable(checkIn: Date, checkOut: Date, adults: number, HotelID) {
 console.log(HotelID);
    var In = checkIn;
    var Out =checkOut;

    var  promises=[];

    var id = []
    HotelID.forEach(element =>
    id.push(element))
    console.log(id)
    var index=0
    var ind = HotelID[0]
      if (ind !== "NULLNULL") {
        console.log('ok')
        const httpOptions = {
          headers: new HttpHeaders({
            'Content-Type': 'application/json',
            'Ama-Pos': '{"agentSine": "A9998WS","agentUserID": "AA","agentDutyCode": "GS","agencyDetails":{"currencyCode": "EUR","identifiers": [{"type": "IATA","id": "QIJI12151"}],"address":{"cityCode": "NCE","countryCode": "FR"}}}'
          })
        };

        let checkIn_param = this.transformDate(checkIn);
        let checkOut_param = this.transformDate(checkOut);
        let apiUrl = `${this.apiRoot_v0}?hotelIds=${ind.toString()}&checkInDate=${checkIn_param}&checkOutDate=${checkOut_param}&roomQuantity=1&adults=2&radius=5&radiusUnit=KM&rateCodes=RAC&paymentPolicy=NONE&includeClosed=true&bestRateOnly=true&view=FULL&sort=NONE`
        let url = `${apiUrl}`;
        console.log(url)
        return await apiCall(url, httpOptions); // here we are calling a function which contain a api call inside this function we are resolving the promise
      } else {

        // here we are not creating a promise bcs here we have simple push operation

        this.resAE.price.push(0)
        this.getAEObservable(checkIn, checkOut, adults, HotelID.slice(1));
        return this.resAE
      }

下面是api调用函数

apiCall(url, httpOptions) {
 return new Promise((resolve, reject) => {
this.httpClient.get<any>(url, httpOptions).toPromise().then(res => {
            console.log(res)
            var price = res.data[0].offers[0].price.total;
            console.log(price)
            this.resAE.price.push( price);
            console.log(this.resAE)
            console.log(id)
            console.log(id.slice(1))
            return this.resAE;
          }).then( resp => {
             this.getAEObservable(checkIn, checkOut, adults, HotelID.slice(1))
             resolve();
         }).catch(this.handleError)
})
}


apiCall(url,httpOptions){
返回新承诺((解决、拒绝)=>{
this.httpClient.get(url,httpOptions.toPromise()。然后(res=>{
console.log(res)
var price=res.data[0]。报价[0]。price.total;
控制台日志(价格)
这个。转售。价格。推送(价格);
console.log(this.rese)
控制台日志(id)
console.log(id.slice(1))
把这个还给我;
})。然后(resp=>{
this.getAEObservable(签入、签出、成人、HotelID.slice(1))
解决();
}).接住(这个.把手错误)
})
}

注释不适用于扩展讨论或调试会话;这段对话已经结束。请确保回答中包含讨论中的任何相关信息。注意评论不是针对扩展的讨论或调试会话;这段对话已经结束。请确保回答中包含讨论中的任何相关信息。注意