What is the benefit of a 'promise' abstraction in CommonJS?

前端 未结 4 685
再見小時候
再見小時候 2020-12-09 18:59

I\'m reading this article and the section on the promise abstraction seems a little overly complicated to me. The following is given as an example:

requestS         


        
4条回答
  •  情书的邮戳
    2020-12-09 19:33

    While it is true that both will ultimately accomplish the same thing, the difference is that your second example is not asynchronous. For example, consider what happens if JSON.parse(...) turns out to be an extremely expensive operation; you'll have to hang until everything's finished, which may not always be what you want.

    That's what promises get you: the powerful ability to defer the computation of the right answer until a more convenient time. As the name suggests, the construct "promises" to give you the result at some point, just not necessarily right now. You can read more about futures and promises work on a larger scale here.

提交回复
热议问题