简体   繁体   中英

CompletableFuture.get() equivalent in javascript Promise?

I recently programming in Typescript/Javascript from JAVA 8. As soon as I encountered async/await , it reminded me of CompletableFuture . In java, regardless of the return type of a method, I can call someMethodReturningFuture.get() inside a method and can block the execution at will. But in JavaScript why do we need to declare a method as async when we want to block on some other asynchronous method using await ? May be there is something so wrong that I am believing till now about Java/CompletableFuture .

Because JS has a synchronous execution model . The resulting behaviour is often described as "single threaded". For sure you could block that "single thread" to wait for the resolution of a promise, but that would block anything² , a behaviour that you probably don't want, and thus it is not part of the language.

²: including browser rendering / reacting to the user, as well as the promise resolution itself ...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM