How does promise.all work?

前端 未结 4 1242
余生分开走
余生分开走 2020-12-06 17:29

I started diggin\' in promises and found interesting Promise.all.

It is stated in MDN that

The Promise.all(iterable) method returns a promise

4条回答
  •  庸人自扰
    2020-12-06 18:01

    The Promise.all(iterables) function returns a single Promise.Here we provide multiple Promises as argument. Promise.all(iterables) function returns promise only when all the promises (argument) have resolved. It rejects when first promise argument reject.

    var promise1 = Promise.resolve(3);
    var promise2 = 42;
    var promise3 = new Promise(function(resolve, reject) {
      setTimeout(resolve, 100, 'foo');
    });
    
    Promise.all([promise1, promise2, promise3]).then(function(values) {
      console.log(values);
    // expected output: Array [3, 42, "foo"]
    });
    

    Syntax:

    Promise.all(func1, func2 [,funcN])

    Parameters:

    Read more at - https://www.oodlestechnologies.com/blogs/An-Introduction-To-Promise.all-Function

提交回复
热议问题