javascript - Promise.all() and then() issues - Stack Overflow

Im having some issues with Promise.all()Im trying to use this chain:Promise.all([this.newContainerLoadi

Im having some issues with Promise.all()

Im trying to use this chain:

Promise.all([
    this.newContainerLoading,
    this.gsapAnim()
])
.then(
    Promise.all([
        this.fadeOut.bind(this),
        this.preload.bind(this)
    ])  
)
.then(this.fadeIn.bind(this))

But for some reason the 2 functions in the second Promise.all() are not even being called? Eg fadeOut() and preload() dont appear to be called at all, and the chain just skips to the final then() and does the fadeIn()

Any ideas as to what Im doing wrong?

Im having some issues with Promise.all()

Im trying to use this chain:

Promise.all([
    this.newContainerLoading,
    this.gsapAnim()
])
.then(
    Promise.all([
        this.fadeOut.bind(this),
        this.preload.bind(this)
    ])  
)
.then(this.fadeIn.bind(this))

But for some reason the 2 functions in the second Promise.all() are not even being called? Eg fadeOut() and preload() dont appear to be called at all, and the chain just skips to the final then() and does the fadeIn()

Any ideas as to what Im doing wrong?

Share Improve this question asked May 18, 2017 at 10:49 MatthewMatthew 9627 silver badges21 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 4

You need to wrap fadeOut and preload in a function so they get called when .all resolved, otherwise they will be called immediately.

Below code shows one correct way to do it

function newContainerLoading() {
  return new Promise((res) => {
    setTimeout(() => {
      console.log('newContainerLoading');
      res()
    }, 1000)
   })
 }

function gsapAnim() {
  return new Promise((res) => {
    setTimeout(() => {
      console.log('gsapAnim');
      res()
    }, 1000)
   }) 
}

function fadeOut() {
    return new Promise((res) => {
    setTimeout(() => {
      console.log('fadeOut');
      res()
    }, 1000)
   })
}

function preload() {
  return new Promise((res) => {
    setTimeout(() => {
      console.log('preload');
      res()
    }, 1000)
   })  
}


function fadeIn() {
 console.log('fadeIn')
}


Promise.all([
    newContainerLoading(),
    gsapAnim()
])
.then(()=> 
    Promise.all([
        fadeOut(),
        preload()
    ])  
)
.then(fadeIn)

bind is used incorrectly here. The result of .bind(this) is bound function. It is not being called, unless it is called explicitly like this.fadeOut.bind(this)().

The purpose of using bind with promises is to use bound functions as callbacks. Promise.all doesn't accept callbacks, but then does. And Promise.all returns a promise, so it has to be wrapped with arrow function. It should be:

Promise.all([
    this.newContainerLoading,
    this.gsapAnim()
])
.then(() => 
    Promise.all([
        this.fadeOut(),
        this.preload()
    ])  
)
.then(this.fadeIn.bind(this))

You should declare the then section in the inner promise and use a variable to store the parent this, see following please:

var self = this;

var newContainerLoading = "dummy";
function gsapAnim(){
  console.log("gsapAnim");
  return "gsapAnim";
}
function fadeOut(){
  console.log("fadeOut");
  return "fadeOut";
}
function preload(){
  console.log("preload");
  return "preload";
}
function fadeIn(){
  console.log("fadeIn");
  return "fadeIn";
}

Promise.all([
    this.newContainerLoading,
    this.gsapAnim()
])
.then(values => {
      Promise.all([
          self.fadeOut(),
          self.preload()
      ]).then(values => {console.log(values)})
   }
)
.then(self.fadeIn.bind(self))

If you want to keep the chain, you should call the last Promise when the first is pleted, see following:

var self = this;

var newContainerLoading = "dummy";
function gsapAnim(){
  console.log("gsapAnim");
  return "gsapAnim";
}
function fadeOut(){
  console.log("fadeOut");
  return "fadeOut";
}
function preload(){
  console.log("preload");
  return "preload";
}
function fadeIn(){
  console.log("fadeIn");
  return "fadeIn";
}

Promise.all([
    this.newContainerLoading,
    this.gsapAnim()
])
.then(values => {
      Promise.all([
          self.fadeOut(),
          self.preload()
      ]).then(self.fadeIn.bind(self))
   }
)

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745161050a4614391.html

相关推荐

  • javascript - Promise.all() and then() issues - Stack Overflow

    Im having some issues with Promise.all()Im trying to use this chain:Promise.all([this.newContainerLoadi

    3小时前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信