3

Whats the difference between Polymers this.async, Promise.then and setTimeout function?

My understanding:

this.async and Promise.then moves a Task to the end of the current Stack and setTimeout is handled as new Task and executed in the next loop when the eventloop takes a new task from the queue?

Please correct me if I am wrong.

simplesystems
  • 839
  • 2
  • 14
  • 28
  • [this](https://jakearchibald.com/2015/tasks-microtasks-queues-and-schedules/) or [this](https://blog.risingstack.com/writing-a-javascript-framework-execution-timing-beyond-settimeout/) might be helpful reads – Damon Dec 08 '17 at 21:43

1 Answers1

4

TLDR: Yes, but note this.async uses setTimeout if a timeout is specified.


  • Polymer.Async.run (this.async) without timeout - queues a microtask (via a MutationObserver callback)
  • Polymer.Async.run (this.async) with timeout - queues a macrotask
  • Promise.then - queues a microtask
  • setTimeout - queues a macrotask

Difference between microtask and macrotask within an event loop context

tony19
  • 125,647
  • 18
  • 229
  • 307
  • this.async queues a microtask and Promise.then creates a microtask? Is there a difference between - queues and creates? – simplesystems Dec 10 '17 at 09:30
  • Things are named (i.e. namespaced) a bit more explicitly starting with Polymer 2: https://github.com/Polymer/polymer/blob/master/lib/utils/async.html -- You decide, via `Polymer.Async.[namespace].run`, which functionality you want. – craPkit Dec 11 '17 at 14:46
  • 1
    @simplesystems in that context, there's no difference between *queue* and *create*. – craPkit Dec 11 '17 at 14:52