const sleep = ms => new Promise((resolve, reject) => setTimeout(resolve, ms))
Because this doesn't reject, I like to write it shorter like this: const sleep = t => new Promise(r => setTimeout(r, t))
i always used this
I do this exactly, except without reject.
This blocks bot.sendVideo
do bot.sendVideo first, then sleep, then rest
setTimeout(() => /* action after x seconds */, x * 1000); // action to be performed right now
Yeah, this blocks sendVideo function itself, doesn't it? I am calling sendVideo inside for of loop, and I need to keep uploading to twitter with 1 minute intervals and to telegram without any delay
setTimeout is non blocking afaik
await sleep(..) will stop the function exection though
yes and vanilla setTimeout wont!
i think you should make a queue for videos to be sent to twitter and you use setInterval to periodaclly check if there are any videos in the queue and send one if there is any
Can't I have one general queue for twitter as well as telegram?
if you can send videos to telegram instantly, you shouldn't need a queue for that
I collect the videos that weren't sent yet and then send them away
what you probably want here is const twitterQueue = []; setInterval(() => { const video = twitterQueue.shift(); if (video) { sendTwitterVideo(..); } }, 60 * 1000); const sendVideo = async (..) => { // .. twitterQueue.push(..); };
Thanks, I will try this
Обсуждают сегодня