evan361425 / evan361425.github.io

我的資料整理處
https://evan361425.github.io
0 stars 0 forks source link

The order of js asynchronous function. #22

Closed andywu0913 closed 2 years ago

andywu0913 commented 2 years ago

Hi evan,

I found your notes by chance and was pretty helpful to me.

After I played around with the code I found that the actual output was not the same as yours.

(executed from Chrome's console)

do first thing
do another thing
fire try-catch!
finish project!

(the notes) https://github.com/evan361425/evan361425.github.io/blob/ca8ae6503c153fe120378157ea75d94e1bc74e9e/src/essay/code/node.js-best-practice.md?plain=1#L47-L56

I think it's because the await callback(); would wait for the promise to be resolved but in the end got an exception. So it passed this exception to its caller(await method();).

The caller then waited for the promise and got the exception too. But this time the exception was caught so it printed out "fire try-catch!" and "finish project!" respectively.

FYI. :)

Andy

evan361425 commented 2 years ago

Hi, @andywu0913. I just added the PR (#23 ) for fixing it.

The main problem here is I added await to wait the asynchronous process done as you pointed out.

Thanks a lot for your contribution 🙏! Feel free to comment and advise on that PR, I'll merge it after we both accept it!