0
let iconsLoaded = new Promise((resolve, reject) => {
  new Promise.all(
    Object.keys(icons).map(iconName => {
      const Provider = icons[iconName][2] || defaultIconProvider; // Ionicons
      return Provider.getImageSource(
        iconName.replace(replaceSuffixPattern, ''),
        icons[iconName][0],
        icons[iconName][1]
      )
    })
  ).then(sources => {
    Object.keys(icons)
      .forEach((iconName, idx) => iconsMap[iconName] = sources[idx])

    // Call resolve (and we are done)
    resolve(true);
  })
});

Please help me understand above code about Promise.all()

I know this Promise object has an call() method,but as I know it's a static method, it should not be used as above,eg: new Promise.all(),but it give me no error. Why-why-why...

Here is source code:

the code above has no error

But, my code as following was wrong..give me an error

let p2 = new Promise(function(resolve, reject){
    console.log('p2 body')
  resolve('p2 resolve')
})

let p1 = new Promise(function(resolve, reject){
    console.log('p1 body')
  resolve('p1 resolve')
})

var p = new Promise(function(resolve, reject){
   new Promise.all([p2,p1])
   .then(function(){
    console.log('all(p2, p1) then')
  })
    console.log('p body')
})
Jeff Bowman
  • 90,959
  • 16
  • 217
  • 251
acoder
  • 249
  • 5
  • 14
  • Promise in an object isn't it this can help you https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Promise – Vinod Louis Apr 07 '17 at 10:35
  • 1
    You're confusing `(new Promise()).all()` and `new Promise().all()`. The first means "construct a Promise and invoke 'all' on that object", while the second is "invoke Promise.all() and then construct an instance of whatever function it returns". – Eduard Malakhov Apr 07 '17 at 10:38
  • the code above has no error But, i wrote code as following was wrong..give me an error let p2 = new Promise(function(resolve, reject){ console.log('fuck p2') resolve('p2 resolve') }) let p1 = new Promise(function(resolve, reject){ console.log('fuck p1') resolve('p1 resolve') }) var p = new Promise(function(resolve, reject){ new Promise.all([p2,p1]) .then(function(){ console.log('fuck then') }) console.log('fuck') }) – acoder Apr 07 '17 at 10:47
  • 1
    "*wrong..give me an error*" - **which** error? Please post it. – Bergi Apr 07 '17 at 12:05
  • Which promise implementation (library) are you using? – Bergi Apr 07 '17 at 12:09
  • Oh, and avoid the [`Promise` constructor antipattern](http://stackoverflow.com/q/23803743/1048572?What-is-the-promise-construction-antipattern-and-how-to-avoid-it)! – Bergi Apr 07 '17 at 12:51
  • here has an same question,,but no resolve https://forums.meteor.com/t/difference-between-promise-returned-with-or-without-keyword-new/28433/2] – acoder Apr 07 '17 at 16:24
  • When I try the code at the top I get the same error: *"TypeError: Promise.all is not a constructor"*. – trincot Apr 07 '17 at 18:51
  • I'm voting to close this question as off-topic because it contains profanities. – Roamer-1888 Apr 07 '17 at 23:26
  • Profanity removed per [meta recommendation](https://meta.stackoverflow.com/q/255188/1426891). acoder, please be mindful that professionals use this site at work, and that appropriate language may allow this question to get more positive attention. – Jeff Bowman Apr 09 '17 at 05:37
  • oh,sorry and thanks – acoder Apr 10 '17 at 01:48

0 Answers0