Execute code after calling each function

I have a function scrolltobottom()that runs after each function is executed.

How can I reorganize the code so that I do not need to type scrolltobottom()everywhere?

Preferably without using 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()
}
+4
source share
1 answer

You can create a decorator function

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

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

Source: https://habr.com/ru/post/1684338/


All Articles