0

I know javascrip is a single thread application. I think to implement asynchronous programing it execute different part of a program synchronously.

In the case of promises it does not stop the execution to resolve the promises. It just execute the other part and my question is what happend when the promise resolves. It just stop the current execution and start the then part of the promise or execute the then part only after completing the current execution

PranavPinarayi
  • 3,037
  • 5
  • 21
  • 32

2 Answers2

0

No. The current queue of code will run before coming back to fulfill promises. This includes the 'then' method.

zfrisch
  • 8,474
  • 1
  • 22
  • 34
0

Promises use "continuation-passing style" ("CPS").

The node approach to CPS is that asynchronous functions accept callbacks. Like so:

const FS = require('fs')
FS.readFile( path, 'utf8', function callback(error, data) {
  // FS.readFile will invoke this function with an error or the file contents
  // once the read operation is complete
})

An explicit promise (i.e. not async/await), looks pretty similar:

const myPromise = new Promise(function callback(resolve, reject) {
    // invoke the non-blocking thing, then use resolve or reject to pass something onward
} )

The callback function is a "continuation," and you provide that continuation to the asynchronous function knowing that it will eventually be invoked with whatever is the result of the non-blocking task.

Promises often look similar, at least superficially. Promise polyfills (which are often not needed) literally work by using some hairy but non-magical CPS code, plus timeouts or events, to provide the same interface. I suspect that modern JS engines use some kind of lower-level mechanism that does not lean on CPS to wire the data into the "callback."

Tom
  • 8,509
  • 7
  • 49
  • 78