Understanding JS Promises

后端 未结 7 1106
梦如初夏
梦如初夏 2020-11-29 05:50

I would like to get a deeper understanding of how Promises work internally. Therefore I have some sample code:

var p1          


        
7条回答
  •  遥遥无期
    2020-11-29 06:33

    Normally code is synchronous - one statement executes like (fileopen) and there is a guarantee that the next statement will execute immediately afterwards like filewrite()

    but in asynchronous operations like nodejs, you should assume that

    • you have no idea when the operation will complete.
    • You can't even assume that just because you send out one request first, and another request second, that they will return in that order
    • Callbacks are the standard way of handling asynchrnous code in JavaScript
    • but promises are the best way to handle asynchronous code.
    • This is because callbacks make error handling difficult, and lead to ugly nested code.
    • which user and programmer not readble easily so promises is the way

提交回复
热议问题