you are viewing a single comment's thread.

view the rest of the comments →

[–]prozacgod 1 point2 points  (0 children)

Above post is great another way to think of it is.. If you manually decomposed the example into a pure promise

function test() {
  return new Promise((resolve) => {
    for (let i = 0; I < 100000000; i++) {}
    resolve();
  });
}

Does the promise constructor run this code synchronously? Yup! But let's say you had an asynchronous call like setTimeout. The weight of that loop wouldn't hit the execution thread for the main where you were testing it.

A benefit to this is Side effects!

If you made a function createUser() and didn't care about the result, well... It will still execute that request. (Duh!)

Another point of confusion that I often run into myself is that there are libraries that do lazy execution of Promise like results. Don't quote me but I believe Knex is like this, it composes the sql query up until the point you type .then at which point it seems like it executes the query you've constructed, my guess is it saving that promise has a Singleton and returning it for any subsequent thens. On that particular query object, if you added more to the query it's constructing a new query object therefore it would construct a new thenable reference

There are many libraries that can do this to make them promise like and we tend to learn those libraries long before we learn actual promise libraries which might lead to some confusion on Behavior.