You are reading a single comment by @luwar and its replies. Click here to read the full conversation.
  • 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();
    
About

Avatar for luwar @luwar started