1

Im a little lost and I think Im hitting "cant see the wood for the trees syndrome".

Im a JS NooB and Im trying to understand how to call a set of JS functions (which return promises) in order. Ive done some reading up and have decided that given Im using Node I should use something like bluebird to manage the promises..

What I cant work out is why this piece of code doesnt work

var Promise = require("bluebird");
// My Promise enabled function from oReily Safari book
function countdown(seconds, timername) {
    return new Promise(function (resolve, reject) {
        console.log('Countdown : Starting Countdown ' + timername);
        for (let i = seconds; i >= 0; i--) {
            setTimeout(function () {
                if (i > 0)
                    console.log(timername + ' ' + i + '...');
                else
                {
                    console.log('countdown '+timername+' now=='+i+' resolving!');
                    resolve(console.log("Countdown : timename="+timername+" ended"));
                }
            }, (seconds - i) * 1000);
        }
    });
}

/*Basic test of promise */
/* when this is run I expected countdown(5, 'Basic : Timer1') to execute and then when **resolved** THEN countdown(5, "Basic Timer2") to execute. 
 *however what I see is both timers executing at the same time.. 
 */

console.log('Basic : Countdown promise test');
countdown(5, 'Basic : Timer1').
        then(function ()
        {
            /*Success */
            console.log("Basic : Ended Successfully");
        }).
        then(countdown(5, "Basic : Timer2")
                );

When I run this I am expecting countdown(5,'timer1') to execute first and then , only when timer1 has finished, will timer2 get executed..

However when I run this I get

Basic : Countdown promise test
Countdown : Starting Countdown Basic : Timer1
Countdown : Starting Countdown Basic : Timer2
Basic : Timer1 5...
Basic : Timer2 5...
Basic : Timer1 4...
Basic : Timer2 4...
Basic : Timer1 3...
Basic : Timer2 3...
Basic : Timer1 2...
Basic : Timer2 2...
Basic : Timer1 1...
Basic : Timer2 1...
countdown Basic : Timer1 now==0 resolving!
Countdown : timename=Basic : Timer1 ended
countdown Basic : Timer2 now==0 resolving!
Countdown : timename=Basic : Timer2 ended
Basic : Ended Successfully
Done.

Im lost..

Many thanks in advance

1
  • you have a tiny typo. then(countdow should be then(x=> countdown()) Commented Dec 30, 2016 at 13:55

3 Answers 3

4

The last part of your code has a unintended bug:

    then(countdown(5, "Basic : Timer2") );

This means the result of countdown() is used as callback function. (countdown function is directly executed)

instead use

then(function(lastResult){ countdown(5, "Basic : Timer2") });

the variable lastResult will have the returned value from the earlier promise in the chain.

Sign up to request clarification or add additional context in comments.

1 Comment

thank you! specifically thank you for explaining "why" . I didnt realise the parameter to "then" is a callback function, vs a function call itself..
1
console.log('Basic : Countdown promise test');
countdown(5, 'Basic : Timer1').
         then(function ()
            {
                /*Success */
                console.log("Basic : Ended Successfully");
                return countdown(5, "Basic : Timer2");
            }).
            then(function(){
                 console.log("Finish!");
             });

Comments

0

You can try this project: https://github.com/LvChengbin/sequence

Sequence.all( [
    () => countdown( 5, 'Basic : Timer1' ),
    () => countdown( 5, 'Basic : Timer2' )
] )

And you can use the second argument of Sequence.all method to specify an interval between every two steps.

You can read its documentation to get more information.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.