Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
74 views
in Technique[技术] by (71.8m points)

javascript - do asynchronous calls in order and repeating on error

i'm curently trying to implement a discord bot using https://github.com/izy521/discord.io that reacts to all messages with letters in a specific order. if one of those calls should fail, it is supposed to repeat it again until it succeeds before moving on to the next letter.

in my example im trying to react with the letters "A" and "G" in that particular order, but when spamming messages the reactions will somtimes not be in order.

async function reactUntilSuccess(mID, channelID, letter){
        await bot.addReaction({channelID,messageID: mID,reaction: letter},
            async function(err,res){
                        
                if (err==null){
                    //reaction sucessul, continue to next letter
                    return;         
                }
                else {
                    //wait a bit and try again
                    await sleep(200)
                    await reactUntilSuccess(mID, channelID, letter)
                }   
            })
            
            
        
}   

//reacts with A and G to message
async function reactString(mID, channelID){
    await asyncForEach(["??","??"], async (letter) => {
    
        //is supposed to wait until last letter is finished before reacting with next letter
        await reactUntilSuccess(mID, channelID, letter)
        
        await sleep(1000)
    })
}

async function asyncForEach(array, callback) {
  for (let index = 0; index < array.length; index++) {
    await callback(array[index], index, array);
  }
}


bot.on('message', function (user, userID, channelID, message, evt) {
    mID = evt.d.id;
    reactString(mID, channelID, message);
    
})  

how do i make it so that my reactString function actually waits until the previous reactions are done?

question from:https://stackoverflow.com/questions/65840416/do-asynchronous-calls-in-order-and-repeating-on-error

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...