Javascript 在调用每个函数后执行代码

Javascript 在调用每个函数后执行代码,javascript,vue.js,Javascript,Vue.js,我有一个scrolltobottom()函数,它在每个函数执行后运行 如何重构代码,使我不必到处键入scrolltobottom() 最好不使用Jquery async function initBot () { let botui = BotUI('homepage-bot', { vue: Vue }) let scrolltobottom = () => { let ele = document.getElementById('botui') d

我有一个
scrolltobottom()
函数,它在每个函数执行后运行

如何重构代码,使我不必到处键入
scrolltobottom()

最好不使用Jquery

async function initBot () {
  let botui = BotUI('homepage-bot', {
    vue: Vue
  })

  let scrolltobottom = () => {
    let ele = document.getElementById('botui')
    document.body.scrollTop = ele.clientHeight
  }

  await botui.message.bot({"Hello"})
  await scrolltobottom()

  await botui.message.bot({"Do you like apple or orange?"})
  await scrolltobottom()

  await botui.message.button({"Orange", "Apple"})
  await scrolltobottom()
}

您可以创建一个decorator函数

function decorateWith(targetFn, fn){
  return function(){
    targetFn.apply(this, arguments);
    fn();
  }
}

[ botui.message.bot,
  botui.message.button ]
  .forEach(fn => decorateWith(fn, scrolltobottom));

如果所有其他函数都是
botui.message.bot
,而不是
botui.message.bot
botui.message.button
,那么您只需将
滚动框放到
botui.message.bot
中,为什么
要等待一个同步函数?