-2

I am new to JS and wanted to understand Promise is JS. So far, I got that Promises are not asynchronous, that is, as you can see in the code below new Promise( function ( ) { console.log( 'b' ); setTimeout( function ( ) { console.log( 'D' ); }, 0 ); } ); When promise object is returned only setTimeOut is executed asychronously and promise serves as a wrapper for that setTimeOut. The question is why need to wrap asynchronous code like setTimeOut inside promise or Ajax requests done with XHR object inside promise.

MMMMMM
  • 19
  • 4
  • 2
    Since the promise shown is never resolved that example is not a good one. Also promises ***are*** asynchronous so your understanding is incorrect about that – charlietfl Jul 13 '19 at 12:08
  • @charlieftl I just told the OP the opposite in his previous question :) – Jonas Wilms Jul 13 '19 at 12:09
  • @charlieftl I don't need examples, I know the spec (all relevant parts). I think it is easier for beginners to understand, if we say that promises aren't async, but that they are wrappers around callbacks called asynchronously. If you read the OPs previous question you get the reasoning for that – Jonas Wilms Jul 13 '19 at 12:15
  • @charlietfl, thank you for your comments please can you read this promise is said to be synchronous https://stackoverflow.com/questions/36588775/are-javascript-promise-asynchronous – MMMMMM Jul 13 '19 at 12:15
  • @JonasWilms guessing you told them the code it executes is synchronous which makes sense... as stated by op it is fuzzy – charlietfl Jul 13 '19 at 12:18
  • @charlietfl, hey dude, I just want to get everything right, if I use the code like return new Promise( function (resolve) { console.log("promise created synchronously"); setTimeout( function ( ) { console.log("callback called asynchronously and promise resolved"); resolve(); } Is the promise object passed into web api then queue then is pushed into stack to be executed? How does the above code works? I hope for your help – MMMMMM Jul 13 '19 at 12:42
  • Code run inside new Promise is synchronous but accessing the resolved values using `then()` is asynchronous – charlietfl Jul 13 '19 at 12:47

1 Answers1

0

For sure you don't have to use Promises, you can do everything just with callbacks. But callbacks itself don't really scale well, in the sense that multiple of them don't work together that well.

For example, lets take an asynchronous function that creates a new user account:

  User.create("jonas", result => {
    alert("New account created");
  });

Now, lets create 10 accounts ... Oh right, how does that work? Try it, and you'll see that it is far more complicated. And thats were Promises get relevant. If User.create() would create a promise, we can easily use Promise.all or Promise.race on them, chain them with .then(...) ...

  Promise.all([
    User.create("jonas"),
    User.create("MM")
  ]).then(result => {
    alert("Two users were created");
  });

Sidenote: Promises only make sense if you resolve them somewhen, so a good example would be:

  function timer(time) {
    return new Promise( function (resolve) { 
      console.log("promise created synchronously"); 
      setTimeout( function ( ) {
       console.log("callback called asynchronously and promise resolved");
       resolve();
     }, time); 
   });
 }

timer(100).then(/*...*/);
Jonas Wilms
  • 132,000
  • 20
  • 149
  • 151