1
C-sucks
3y

how do callback functions work in js?

Comments
  • 10
    Functions are objects
    You pass them like any other object
    You call it
    It triggers
  • 1
    Like confusing bullshit that makes little sense.

    And just like @Hazarth said.
  • 2
    Type ExampleOfCallback = (params: any, callback: () => void) => void

    Then applied

    Const example: ExampleOfCallback = (params, callback) => {
    Console.log(‘params’, params)
    callback()
    }

    Example({}, () => console.log(‘callback called’))
  • 2
    you want to trigger some function after some other function completes execution, if your function is asynchronous, you can't just write it one after another as it will not wait for finishing, so, you pass your function as parameter which is triggered after execution.

    Other option is async/await which is much cleaner.
  • 1
Add Comment