0

I'm learning Promise. I wrote this simple code to practice the theoretical concepts learned. But it doesn't behave as expected. In my understanding the Promise.resolve is in a resolve state, thereby the then() is executed immediately and the object is passed as value. The handler of the then() return a Promise because i want to seralize the asynchronous code. In the Promise the synchronous code is execuded instead the setTimeout should be put in the Job Queue. Then, the synchronous execution code continue but the following then doesn't exec because the calling Promise is in a pending state. Thus only when the callback of the setTimeout run the Promise become in resolve state and in the meanwhile pass the value Object. I would expect that the second console.log in the second then() method to perform after 5 seconds but it isn't the case. The three console.log print immediately.

 Promise.resolve({id:"1",type:"person"})
      .then((value) => new Promise((resolve,reject)=>{
         console.log(value);
         value.id="2";
         setTimeout(resolve(value),5000);
         }
      ))
      .then((value) => new Promise((resolve,reject)=>{
         console.log(value);
         value.id="3";
         setTimeout(resolve(value),5000);
         }
      ))
      .then((value)=>console.log(value));

This code works, but without to pass the object between the Promises:

 Promise.resolve({id:"1",type:"person"})
      .then((value) => new Promise((resolve,reject)=>{
         console.log("1");
         value.id="2";
         setTimeout(resolve,5000);
         }
      ))
      .then(() => new Promise((resolve,reject)=>{
         console.log("2");
         
         setTimeout(resolve,5000);
         }
      ))
      .then(()=>console.log("3"));

Where am I wrong? Which puzzle piece am I missing?

Nick
  • 1,439
  • 2
  • 15
  • 28
  • 2
    you're not passing a function to settimeout in the first code ... your calling resolve immediately, and passing the result of calling resolve to settimeout (which is undefined so the setimeout doesn't receive a function callback) ... try `setTimeout(resolve, 5000, value)` – Jaromanda X May 07 '20 at 23:13
  • @JaromandaX Yes, you are right. Here is late, better to go to sleep. However the parameters of the setTimeout should be valued immediately and the resolve(value) function call cause that setTimeout isn't executed. – Nick May 07 '20 at 23:22
  • erm, no, that didn't make sense - what I suggested is the same as `setTimeout(() => resolve(value),5000);` if that helps – Jaromanda X May 07 '20 at 23:22
  • I understood your workaround. In the previous comment I was trying to explain the behavior of my first code with bug. – Nick May 07 '20 at 23:29
  • oh, I explained that in my first comment too :p – Jaromanda X May 07 '20 at 23:33
  • Yes you are right again. resolve(value) cause Promise become in a resolve state, and resolve(value) return undefined as value. But the setTimeout is still executed, the callback is still put in the Job Queue but the callback is undefined thus it does nothing once elapsed 5 seconds. Thanks now everything it's clear. – Nick May 07 '20 at 23:40

0 Answers0