Why use callback in JavaScript, what are its advantages?

后端 未结 10 1745
甜味超标
甜味超标 2020-12-04 18:02

Could someone explain, why do we use callback in JavaScript? I found examples, but they could be implemented by using the normal functions. What is the advantage of using it

10条回答
  •  爱一瞬间的悲伤
    2020-12-04 18:21

    Because the javascript being executed is Asynchronous, therefore if you just put any old function after making the asynchronous request, it will likely be called before the original request completes. The original request will return as soon as it BEGINS (is sent out), not completes.

    If you need to do something with the result of the asynchronous request, or chain together requests, etc you will need a callback to ensure the next step doesn't begin before the previous step is finished.

提交回复
热议问题