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(..)
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();
@luwar started
Espruino is a JavaScript interpreter for low-power Microcontrollers. This site is both a support community for Espruino and a place to share what you are working 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?
It works when I wrap the code into a function call:
Second question: The results of the promises seems to be circular shifted.