skip to Main Content
function foo(num) {
  return new Promise((resolve) => console.log(num));
}

foo(1).then(() => {
  foo(3);
});

The function foo returns an immediately resolve promise. "1" is printed, but why doesn’t the chain continue with printing "3"?

2

Answers


  1. You need to resolve Promise like this:

    function foo(num) {
      return new Promise((resolve) => {
        console.log(num)
        resolve()
      });
    }
    
    foo(1).then(() => {
      foo(3);
    });
    Login or Signup to reply.
  2. @JobHunter69 The function foo indeed creates a promise but does not explicitly call resolve() to resolve the promise. Therefore, the promise returned by foo never completes or resolves.

    function foo(num) {
      return new Promise((resolve) => {
        console.log(num);
        resolve(); 
      });
    }
    
    foo(1).then(() => {
      return foo(3);
    }).then(() => {
      console.log("Chain continued successfully.");
    });
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search