3

How does JavaScript execute Promises async when JS is single threaded? I know how to use Promises, but it is still unclear to me how it works behind the scene.

daniel
  • 334
  • 3
  • 6
  • in simple terms, the callbacks for each outcome are stored in an array. when the deferred is resolved on some event, the callbacks are executed. – Kevin B May 22 '14 at 17:42
  • the same way setTimeout works.the execution is just deferred,doesnt need another thread to defer something.wether there is IO or not , a promise resolution or failure must be deferred,or it's not a promise. – mpm May 22 '14 at 17:43
  • 3
    see: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/EventLoop – SimpleJ May 22 '14 at 17:44
  • asynchronous != multithreaded – Bergi May 22 '14 at 18:12

1 Answers1

2

Promises are just a callback queue assigned to a lookup. Once you resolve the promise, it iterates over all callbacks which have been assigned via then or done.

AlienWebguy
  • 76,997
  • 17
  • 122
  • 145
  • 1
    A [littlebit *more* than callbacks](http://stackoverflow.com/a/22562045/1048572), but yes - with the same limitations. – Bergi May 27 '14 at 10:53