0

Well i have this function here that runs on intervals and what it does is, when it runs, it sets 2 settimeout.

Here's the problem, an asynchronous function that should be called after the 2nd time out is called before the 2nd timeout is even triggered. This is my code.

    var runner = Interval.run(function() {
    //-212965881
    bot.sendMessage('-212965881', "Drop usernames now").then(function(){
        ready = 1;
        list = {};
        console.log(ready);
        setTimeout(function(){
            return bot.sendMessage('-212965881',"Round has begun. Please start liking now. Leech check will begin in 1 minute");
        }, 10000);
    }).then(function(){
        ready = 0;
        setTimeout(function(){
            return bot.sendMessage('-212965881',"Leech check has begun!");
        }, 15000);
    }).then(function(){
        //This one fires before 15 seconds
        let msg = {chat:{}};
        msg.chat.id = '-212965881';
        return bot.event('/check', msg);
    }).catch(function(err){
        console.log(err);
    });
},  20000);

Not sure why this happens. Perhaps im going about it the wrong way. Can anyone throw some light on this? Thanks

Haider Ali
  • 918
  • 2
  • 9
  • 26
  • Possible duplicate of [How do I return the response from an asynchronous call?](https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call) – Patrick Roberts Sep 28 '17 at 03:20
  • 2
    `setTimeout()` schedules something to run asynchronously later, it doesn't pause execution. Also the return value from any function you pass to `setTimeout()` is ignored. – nnnnnn Sep 28 '17 at 03:20

2 Answers2

0

This happens because the code inside your then handlers are creating un-returned asynchronous code, or basically "breaking out" of the Promise chain without notifying the Promise chain of the activities.

You need to wrap your setTimeout inside of a Promise constructor and then also make sure you wait for the interior bot.sendMessage invocations to finish by resolving them inside your new Promises

Change it to use Promise constructors, see Resolving a Promise

var runner = Interval.run(function() {
    //-212965881
    bot.sendMessage('-212965881', "Drop usernames now").then(function(){
        ready = 1;
        list = {};
        console.log(ready);
        return new Promise((resolve) {
            setTimeout(function(){
                resolve(bot.sendMessage('-212965881',"Round has begun. Please start liking now. Leech check will begin in 1 minute"))
            }, 10000);
        });
    }).then(function(){
        ready = 0;
        return new Promise((resolve) {
            setTimeout(function(){
                resolve(bot.sendMessage('-212965881',"Leech check has begun!"))
            }, 15000);
        });
    }).then(function(){
        //This one fires before 15 seconds
        let msg = {chat:{}};
        msg.chat.id = '-212965881';
        return bot.event('/check', msg);
    }).catch(function(err){
        console.log(err);
    });
},  20000);
Cameron
  • 1,524
  • 11
  • 21
0

because the asynchronous functions you were trying to invoke are returned few seconds later. The three .then you wrote works on the first Promise repeatly.

your can use co module or ES6 async/await to control multiple Promise.

seeLuck
  • 133
  • 8