DEV Community

Discussion on: Angular: Observable v/s Promise

Collapse
 
m__mdy__m profile image
Genix

While it's true that a Promise starts executing immediately upon creation, it's important to clarify that the execution happens asynchronously. The term "eager" can be misleading because it implies that the Promise blocks the main thread, but that's not the case. Promises do not block the main thread; they begin execution as soon as they are created, but they resolve asynchronously—meaning the callback functions (.then(), .catch(), etc.) are queued to run later when the JavaScript event loop is free.

For example:

const promise = new Promise((resolve, reject) => {
  setTimeout(() => {
    resolve('Resolved!');
  }, 1000);
});

console.log('Promise created');
promise.then(value => console.log(value));
Enter fullscreen mode Exit fullscreen mode

In this case, the Promise starts executing as soon as it’s created, but the resolve() function waits for 1 second before returning the value asynchronously.

Collapse
 
ankushgoyal11 profile image
Ankush Goyal

Thank you for your insightful comment! You are absolutely correct that Promises in JavaScript start executing immediately upon creation, but they do so asynchronously.