Javascript JS:如何推迟函数的执行

Javascript JS:如何推迟函数的执行,javascript,vuejs2,vue-component,Javascript,Vuejs2,Vue Component,正在尝试装载Vue.js组件,但第二个函数(eventSelection)的调用似乎比预期的更早。我应该怎么做才能使它仅在第一个函数完成后执行 ... mounted() { this.getAllEvents() this.eventSelection() }, methods: { getAllEvents: function () { console.log("Cheguei aqui"); getEvents() .then( function (r

正在尝试装载Vue.js组件,但第二个函数(
eventSelection
)的调用似乎比预期的更早。我应该怎么做才能使它仅在第一个函数完成后执行

...
mounted() {
  this.getAllEvents()
  this.eventSelection()
},

methods: {
  getAllEvents: function () {
    console.log("Cheguei aqui");
    getEvents()
    .then( function (res) {
      console.log("Entrei then")
      this.events = res.data
    }.bind(this))
    .catch( function (err){
      debugger;
      console.error('WeekSimulation, getEvents() ', err)
      this.events = ["Seu chefe o convida para um happy hour com os diretores no final do expediente.  Ao mesmo tempo, você recebe uma mensagem de seu cônjuge lembrando da apresentação no colégio do seu filho. O que você faz?", "Você tem muito trabalho a fazer, porém o tempo com sua família anda escasso. No final do expediente você escolheria jantar com sua família ou fazer hora extra?",
                         "Você acorda de manhã e seu filho não está se sentindo bem. Ao verificar sua agenda, lembra que tem uma reunião com um novo cliente em uma hora. Você leva seu filho ao médico ou vai para a reunião?", 
                         "Ao checar o seu celular durante uma reunião com os diretores de sua organização, nota que recebeu cinco ligações de seu cônjuge. Você continua na reunião, ou pede para atender o telefone?"]
    }.bind(this))
  },

  eventSelection: function() {
    console.log("Funcao de selecao de evento")
    debugger;
    this.selectedEvent = _.shuffle(this.events)[0]
    console.log(this.selectedEvent)
  }      

}
...

您需要使用
promissions()
。 很抱歉,我无法理解您的代码,以下是一个通用示例:

first = function(){
    var deferred = new $.Deferred();
    console.log("first running");
    deferred.resolve();  // <----------resolve the deferred
    return deferred.promise();
}

second = function(){
    console.log("second running..sigh..");
}

$(document).ready(function() {

    first().then(second);

});
first=函数(){
var DEREFERED=新的$.DEREFERED();
console.log(“首次运行”);
deferred.resolve();//使用一个简单的回调:

 mounted() {
   this.getAllEvents(this.eventSelection);
 }

 getAllEvents: function (cb) {
    ...
    cb();
 },

  eventSelection: function() {
   ...
  }
getAllEvents()
返回
getEvents()
承诺,这样您就可以将另一个
then()
链接到它,并在getEvents()解析后调用
eventSelection()


还要确保在
catch
中添加
返回此事件。事件
以解析初始承诺

getAllEvents
不会执行任何回调函数
promise()
答案,OP。请听这个家伙的代码已经使用承诺,否则您就不会有
then()
methods表示努力,但它有点臃肿否?将this.events设置为getEents的结果,如下所示:
this.events=getEents()。然后(d=>d.data)
在eventSelection中:
this.events.then(events=>this.selectedEvent=\uuuu.shuffle(this.events)[0])
mounted() {
  this.getAllEvents().then(this.eventSelection)

},

methods: {
  getAllEvents: function () {
   // return the promise
   return getEvents()
    .then( function (res) {
      ....
    }.bind(this))


}