How to delay a promise in when function How to delay a promise in when function angularjs angularjs

How to delay a promise in when function


To delay a promise, simply call the resolve function after a wait time.

new Promise(function(resolve, reject) {  setTimeout(function() {    resolve();  }, 3000); // Wait 3s then resolve.});

The issue with your code is that you are returning a Promise and then inside the then of that Promise you are creating another one and expecting the original promise to wait for it - I'm afraid that's not how promises work. You would have to do all your waiting inside the promise function and then call resolve:

Edit: This is not true, you can delay the promise chain in any then:

function promise1() {  return new Promise((resolve) => {    setTimeout(() => {      console.log('promise1');      resolve();    }, 1000);  })  .then(promise2);}function promise2() {  return new Promise((resolve) => {    setTimeout(() => {      console.log('promise2');      resolve();    }, 1000);  });}function promise3() {  return new Promise((resolve) => {    setTimeout(() => {      console.log('promise3');      resolve();    }, 1000);  });}promise1()  .then(promise3)  .then(() => {    console.log('...finished');  })