Bugs in Promise.all(..) ?

Posted on
  • I'm in the process of migrating my modules from using callbacks to promises. The code looks much tidier.

    But I run into trouble with Promise.all(..)

    First question: Why is there no output?

    var promise = new Promise( function(resolve,reject) { resolve(); } );
    Promise.all( [promise] ).then( function() { print( "never called :-(" ); } );
    

    It works when I wrap the code into a function call:

    function start() {
          var promise = new Promise( function(resolve,reject) { resolve(); } );
          Promise.all( [promise] ).then( function() { print( "called :-)" ); } );
    }
    
    start();
    

    Second question: The results of the promises seems to be circular shifted.

    function start() {
          var promiseTwo = new Promise( function(resolve,reject) { resolve(1); } )
          .then( function( n ) { return 2; } );
    
          var promiseFour = new Promise( function( resolve, reject ) { resolve(4); } );
    
          Promise.all( [promiseTwo, promiseFour] ).then( function( numbers ) {
              console.log( "expecting 2, got " + numbers[0] ); // got 4
              console.log( "expecting 4, got " + numbers[1] ); // got 2
          } );
    }
    
    start();
    
  • It could well be an issue - while promises have got quite a lot of use recently, I haven't personally used .all, and I guess there could be some problems. I'll file a bug to look into it - I'm quite busy now so may not get to fix it for a few weeks though.

    In the mean time, you could probably replace Promise.all with your own function if you wanted?

  • Just looked at this, and:

    var promise = new Promise( function(resolve,reject) { resolve(); } );
    Promise.all( [promise] ).then( function() { print( "never called :-(" ); } )
    

    Does work. My guess is the reason you're having problems is you're doing this at the root scope - effectively pasting in one command after the other. In that case, var promise = new Promise( function(resolve,reject) { resolve(); } ); gets called first, and executes on its own. There's nothing linked to it so it just completes and everything is ok.

    Then, when you do Promise.all, it's already been resolved so nothing happens. If you just wrap the two commands in curly braces or execute them in a function, everything's fine. I'll try and add something to cope with that eventuality.

    As for the other bit, at the moment Promise.all doesn't return things in order - just in the order they complete. I'll try and fix that one too.

  • If you get the latest build in an hour or so, this should all be fixed.

  • Post a reply
    • Bold
    • Italics
    • Link
    • Image
    • List
    • Quote
    • code
    • Preview
About

Bugs in Promise.all(..) ?

Posted by Avatar for luwar @luwar

Actions