0

I think I understand the nature of async vs sync. I also understand that javascript includes certain async functions, such as setTimeout, that are able to function because they use a Web API of some sort. I also understand that you are able to make promises using external libraries such as bluebird. This all makes sense.

However, if Promises are async, and ES6 includes primitive Promise objects, then how is that possible if JS is single threaded?

I suppose that either: 1. JS is not purely single-threaded 2. Promises are not async 3. Primitive Promises use a Web API (or some sort of API provided by the environment)

Or none of the 3 above? Appreciate a detailed explanation.

  • @jfriend00 I saw that question that you marked as duplicate. I continued posting the question because I feel that myself, and many others would benefit from a clear response. The top answer in that link contradicts the first comment (as well as others). – Aljosha Novakovic Feb 11 '17 at 08:52
  • Promises don't use threads at all. They are simply a tool for managing the completion of already existing asynchronous operations that may or may not use native threads in their implementation. Also, promises don't run. They are simply a monitoring tool. Asynchronous operations run. Promises just monitor those operations. What about this question is not answered in that other one. Promises don't use threads. Promises by themselves are even asynchronous operations. They are a tool used as a proxy for an actual asynchronous operation. – jfriend00 Feb 11 '17 at 08:56
  • @jfriend00 Actually, `Promise.resolve("World").then(console.log); console.log("Hello")` is asynchronous by itself. It's more like #3 - promises use the same asynchrony interface of the environment that also web apis are using. – Bergi Feb 11 '17 at 11:47
  • Btw, Bluebird is not an "external" library of any sorts, it's written in JS and does use web apis such as `setTimeout` to provide asynchrony for its promises. – Bergi Feb 11 '17 at 11:48
  • @AljoshaNovakovic Why do you think that single-threadedness and asynchrony are mutually exclusive? – Bergi Feb 11 '17 at 11:49
  • Maybe this helps in understanding how JS works: https://developer.mozilla.org/en-US/docs/Web/JavaScript/EventLoop – Felix Kling Feb 11 '17 at 16:35

0 Answers0