Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/hibernate/5.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
Javascript 为什么即使在.then()语句中,推送到数组也不等待异步任务完成?_Javascript_Asynchronous - Fatal编程技术网

Javascript 为什么即使在.then()语句中,推送到数组也不等待异步任务完成?

Javascript 为什么即使在.then()语句中,推送到数组也不等待异步任务完成?,javascript,asynchronous,Javascript,Asynchronous,请原谅/请纠正对词汇表的任何误用:我正在从CORS请求中检索数据,并在结果中循环,在每次迭代中将一个对象推送到一个数组中。我可以看到数组在增长,但一旦$.get()请求完成,我的数组似乎又是空的 我猜想$.get()命令是异步运行的,而第二个console.log()命令只是在get完成之前执行的。但是,我已经将console.log()放在.then()命令中,仍然遇到同样的问题。有人知道这里发生了什么,以及如何修复它吗 Bike = function(dateStolen, frameCol

请原谅/请纠正对词汇表的任何误用:我正在从CORS请求中检索数据,并在结果中循环,在每次迭代中将一个对象推送到一个数组中。我可以看到数组在增长,但一旦
$.get()
请求完成,我的数组似乎又是空的

我猜想
$.get()
命令是异步运行的,而第二个
console.log()
命令只是在
get
完成之前执行的。但是,我已经将
console.log()
放在
.then()
命令中,仍然遇到同样的问题。有人知道这里发生了什么,以及如何修复它吗

Bike = function(dateStolen, frameColors, frameModel, id, imageUrl, manufacturer, serial, location, title, year){
  this.dateStolen = dateStolen;
  this.frameColors = frameColors;
  this.frameModel = frameModel;
  this.id = id;
  this.imageUrl = imageUrl;
  this.manufacturer = manufacturer;
  this.serial = serial;
  this.location = location;
  this.title = title;
  this.year = year;
};

Bike.prototype.addStolenBikesToArray = function(allBikes){
  $.get("https://bikeindex.org:443/api/v3/search?page=1&per_page=25&location=IP&distance=10&stolenness=stolen")
  .then(function(response){
    response.bikes.forEach(function(bike){
      var currentBike = new Bike(bike.date_stolen, bike.frame_colors, bike.frame_model, bike.id, bike.thumb, bike.manufacturer_name, bike.serial, bike.stolen_location, bike.title, bike.year);
      allBikes.push(currentBike);
      // this displays an ever-growing array of bike objects
      console.log(allBikes);
    })
  })
  // this displays an empty array
  .then(console.log(allBikes));
};

Bike.prototype.setColor = function (userName){
  this.userName = userName;
};

exports.bikeModule = Bike;
然后是我的接口文件:

var Bike = require("./../js/stolenBikes.js").bikeModule;

var determineMostStolenManufacturer = function(bikeArray){
  var manufacturerArray = getArrayOfManufacturers(bikeArray);
  console.log(manufacturerArray);
};

var getArrayOfManufacturers = function(bikeArray){
  var manufacturerArray = [];
  bikeArray.forEach(function(bike){
    manufacturerArray.push(bike.manufacturer);
  });
  return manufacturerArray;
};

$(function(){
  var currentBike = new Bike();
  var allBikes = [];
  currentBike.addStolenBikesToArray(allBikes);
});

。然后
需要一个函数作为参数。。。不是调用函数的结果
consoloe.log(blah)
调用console.log,其结果是
未定义
-这是传递给
的内容。然后
-它会忽略它,就像
一样。然后
忽略非函数参数以将其包装到函数中?”console.log(allBikes)’每当你有
foo(bar())
,首先执行
bar
,其结果被传递到
foo
。这意味着执行
console.log(allBikes)
,它返回
undefined
,并将其传递给
。然后
。然而,
。然后
期望一个函数,一旦承诺得到解决,就可以调用该函数。因此,不是
,而是(console.log(allBikes))您想要
。然后(()=>console.log(allBikes))。谢谢,伙计们。这就解决了问题。