How does the new promise API work?

Posted on
  • I would like to use the new Promise API introduced in 1v86 but I'm not able to get even the simplest scenarios to work.
    Should the Espruino promises work like the normal ES6 promises?

    The following code should output 1 1 2 4 but does 1 1 1 1. A bug or a feature? ;-)

    var p = new Promise( function(resolve) { resolve(1); });
    
    p.then( function(value) {
            console.log(value);  // 1
            return value + 1;
    }).then( function(value) {
            console.log(value);  // 2
            return new Promise( function( resolve ) { resolve( 4 ); } );
    }).then( function( value ) {
            console.log( value ); // 4
    } );
    
    p.then(function(value) {
            console.log(value); // 1
    });
    
  • Ahh, yes - i'd say that's a bug then. I'll file an issue for it... I'm amazed nobody else spotted that.

    The whole resolve reject thing works, but I bet it doesn't cope with stacking thens - which is kind of the point :)

  • Hi Gordon

    Did this issue get resolved in an update?

  • Not yet I'm afraid. I was on holiday immediately after that and then again last week, so I spent most of my time playing catch-up on e-mails and making sure the Puck.js stuff was moving along.

  • The relevant issue is this one: https://github.com/espruino/Espruino/iss­ues/894

    I just had a quick stab at this but it'll need a bit more time to get working for multiple calls of p.then. I'd been assuming that it always chained calls, but you can actually call two things at the same time with it.

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

How does the new promise API work?

Posted by Avatar for luwar @luwar

Actions