Take it that your friend is returning to the library to borrow you some books. You would not simply continue to wait but read a magazine or do other activities while waiting. Here's how async/await works in a simple scenario.
Async: All that means is that the information exchange does not happen at the same time but instead, it's like asking a friend to go to the library to get the book while you do your thing.
Await: This is as saying "Only do the following after this is complete." It's like requesting one's friend to convey the message after he/she finishes the book and then move on to another work.
Here's a basic example using async/await in JavaScript:
getBookFromLibrary() is like sending your friend to the library.
readMagazine() is like reading a magazine while you wait.
getBookAndRead() is like coordinating everything, making sure you start reading the magazine first, then get the book when your friend returns from the library.
Thus, async/await lets you write asynchronous code as a sequence in contrast to the regular way of doing multiple tasks at the same time but waiting for one thing to finish before going further like in real life.
0 Comments