标题说明了一切。我不确定我是否了解javascript中的.then()功能。
我有这个全局数组:
<body>
<script>
Script 1 here, checking browser version and using `document.write()` if
unsupported
</script>
<script>
React script here, which shouldn't run/be imported if the above script
found an unsupported browser.
</script>
</body>
我将这段代码嵌套在一个for循环中:
var ticketArray = [];
基本上我想做的是:每次调用let searchOptions = {
from: 'LAX',
to: 'JFK',
departureDate: '2018-12-'+concatZero(i),
partialTrips: true,
sort: 'cost'
};
flightScanner(searchOptions).then(response =>{
let ticket = {
pennyPrice: 0,
duration: '',
depature: '',
arrival: '',
key: '',
legs: []
}
ticket.pennyPrice = response[0].price_pennies;
ticket.duration = response[0].durationSeconds;
ticket.depature = response[0].departureTime;
ticket.arrival = response[0].arrivalTime;
ticket.key = response[0].flight_key;
ticketArray.push(ticket);
});
并给出响应时,从该响应中提取一些特定数据,将其存储到本地对象flightScanner(searchOptions)
中,然后将该本地推入反对我的全局ticket
。
我能够提取数据并将其存储到ticketArray
中,但是ticket
并没有被压入阵列中。为什么是这样?我什至正确使用.then()吗?我假设它与范围有关。任何帮助将不胜感激!
此处的完整代码:https://jsfiddle.net/g0zm2ut6/