Skip to content

Instantly share code, notes, and snippets.

@codeami
Last active October 17, 2016 07:34
Show Gist options
  • Save codeami/2c1363db757994ebfc0d8b52dd345852 to your computer and use it in GitHub Desktop.
Save codeami/2c1363db757994ebfc0d8b52dd345852 to your computer and use it in GitHub Desktop.
JavaScript Asynchronous Execution Concepts: Callbacks vs Promises - Concepts and History

JavaScript Asynchronous Execution Concepts: Callbacks vs Promises - Concepts and History

Dated 10/15/2016

Please read these excellent articles/threads online (presented in order I feel is best for basic understanding) After reading the excertps presented here; please flow the links and read them, esp the comments on each QnA on SO (stackOverflow)

Sync vs Async

http://stackoverflow.com/questions/748175/asynchronous-vs-synchronous-execution-what-does-it-really-mean When you execute something synchronously, you wait for it to finish before moving on to another task. When you execute something asynchronously, you can move on to another task before it finishes.

Small Example:

Let's say playing an audio involves three steps:

Getting the compressed song from harddisk Decompress the audio. Play the uncompressed audio. If your audio player does step 1,2,3 sequentially for every song then it is synchronous. You will have to wait for some time to hear the song till the song actually gets fetched and decompressed.

If your audio player does step 1,2,3 independent of each other, then it is asynchronous. ie. While playing audio 1 ( step 3), if it fetches audio 3 from harddisk in parallel (step 1) and it decompresses the audio 2 in parallel. (step 2 ) You will end up in hearing the song without waiting much for fetch and decompress.

That being, said, in the context of computers this translates into executing a process or task on another "thread." A thread is a series of commands--a block of code--that exists as a unit of work. The operating system can manage multiple threads and assign a thread a piece ("slice") of processor time before switching to another thread to give it a turn to do some work. At its core (pardon the pun), a processor can simply execute a command--it has no concept of doing two things at one time. The operating system simulates this by allocating slices of time to different threads.

Now, if you introduce multiple cores/processors into the mix, then things CAN actually happen at the same time. The operating system can allocate time to one thread on the first processor, then allocate the same block of time to another thread on a different processor.

All of this is about allowing the operating system to manage the completion of your task while you can go on in your code and do other things. Asynchronous programming is a complicated topic because of the semantics of how things tie together when you can do them at the same time. There are numerous articles and books on the subject; have a look!

http://andyshora.com/promises-angularjs-explained-as-cartoon.html http://stackoverflow.com/questions/21518381/proper-way-to-wait-for-one-function-to-finish-before-continuing http://stackoverflow.com/questions/4296505/understanding-promises-in-node-js http://stackoverflow.com/questions/34960886/are-there-still-reasons-to-use-promise-libraries-like-q-or-bluebird-now-that-we

A promise can be in 3 states: pending, fulfilled or rejected. (resolved is an older term used by Q, which means either fulfilled or rejected, but with Promise A+ spec, resolved is not being used as a term any more).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment