How are Promises implemented in Javascript without threads

后端 未结 3 916
失恋的感觉
失恋的感觉 2020-12-04 17:57

Recently, I have been seeing the concept of Promises being implemented in AngularJS and JQuery.

I have seen the implementation of a Futures in Java as in code below,

3条回答
  •  無奈伤痛
    2020-12-04 18:38

    The browser-written native code underneath the JavaScript layer tends to have threads implemented very neatly. As it turns out, that's usually all you need. Promises tend not to be needed for performing actual computation work in JavaScript (although workers make that easier) but for loading outside resources, and getting callbacks when they're done. JS Promises just assign callbacks to those functions like "image.onLoad" and check whether to notify another function.

    Hogan may have summarized it best - event-based programming.

提交回复
热议问题