我知道这个问题已经研究了上千次,但是我尝试了很多解决方案,但仍然没有用。
我想链接来自http get的promise,但是我需要在运行下一个之前等待上一个,因为get方法返回给我一个数字,该数字被推送到数组中,并且需要正确排序。 / p>
我尝试过的方法:将每个promise放入数组中,然后使用reduce方法,但是没有用。 我认为棘手的事情是,我具有获取值发生变化的参数之一的条件。 这些值在一个数组中,所以我试图在等待时执行一次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)
})
}
想法是执行第一个get,填充我的数组,完成后,通过调用函数本身,但删除第一个getID的酒店ID,继续进行下一个get调用。
该数组已填充,因此没有其他问题,我认为这不是很好。
我只是编辑我的问题,说console.log(url)告诉我该URL是按良好顺序创建的
答案 0 :(得分:0)
您可以尝试这样。
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)
})
}