Adi
Adi3y ago

Adi – 14-12 Jan 23

I am trying to upload media but can I add a time interval between first media upload and second media upload?? Like interval of 2secs so that first media upload gets completed and then after 2secs it starts uploading 2nd media. Is it possible to do it? code:
const mediaIds = [];
for (const item of status.images) {
const mediaId = await mediaUpload(item, process.env.CONSUMER_KEY, process.env.CONSUMER_SECRET, access_token_key, access_token_secret);
mediaIds.push(mediaId);
}
const mediaIds = [];
for (const item of status.images) {
const mediaId = await mediaUpload(item, process.env.CONSUMER_KEY, process.env.CONSUMER_SECRET, access_token_key, access_token_secret);
mediaIds.push(mediaId);
}
After successful completion of upload it returns mediaIds which I am storing but I want to add delay between 2 uploads how can I do it? Any suggestions?
6 Replies
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
Adi
Adi3y ago
Yes I tried exactly but the mediaIds which I get I use them in next API call which gets called. So for that next API call as well should I add setTimeout?? Because right now what is happening that next API call is getting executed without mediaIds because mediaIds have some timeout
Unknown User
Unknown User3y ago
Message Not Public
Sign In & Join Server To View
Adi
Adi3y ago
code:
setTimeout(async () => {
if (mediaIds.length === status.images.length) {
const tweet = await client.post("statuses/update", {
status: status.text,
in_reply_to_status_id: lastTweetID,
auto_populate_reply_metadata: true,
media_ids: mediaIds
});
lastTweetID = tweet.id_str;
}
}, status.images.length * 1000);
setTimeout(async () => {
if (mediaIds.length === status.images.length) {
const tweet = await client.post("statuses/update", {
status: status.text,
in_reply_to_status_id: lastTweetID,
auto_populate_reply_metadata: true,
media_ids: mediaIds
});
lastTweetID = tweet.id_str;
}
}, status.images.length * 1000);
In above code see I have added setTimeout but that time is equal to number of images * 1sec How to make sure await client.post("statuses/update") gets called only when mediaIds are present but upload is in still process then I don't want to make that API call and wait so for how much time should I wait? so for safe value I have taken number of images * 1sec assuming it takes 1 sec to upload 1 image and each mediaId gets generated within 1sec code:
const mediaIds = [];
for (const item of status.images) {
setTimeout(async () => {
const mediaId = await mediaUpload(item, process.env.CONSUMER_KEY, process.env.CONSUMER_SECRET, access_token_key, access_token_secret);
mediaIds.push(mediaId);
}, 1000)
}
const mediaIds = [];
for (const item of status.images) {
setTimeout(async () => {
const mediaId = await mediaUpload(item, process.env.CONSUMER_KEY, process.env.CONSUMER_SECRET, access_token_key, access_token_secret);
mediaIds.push(mediaId);
}, 1000)
}
I solved it finally
Adi
Adi3y ago
I was making mistke with loop and async/await. This helped -> https://www.codegrepper.com/code-examples/javascript/promise+in+map+javascript
promise in map javascript Code Example
const mapLoop = async _ => { console.log('Start') const promises = fruitsToGet.map(async fruit => { const numFruit = await getNumFruit(fruit) return numFruit }) const numFruits = await Promise.all(promises) console.log(numFruits) console.log('End') }
reactibot
reactibot3y ago
This thread hasn’t had any activity in 12 hours, so it’s now locked. Threads are closed automatically after 12 hours. If you have a followup question, you may want to reply to this thread so other members know they're related. https://discord.com/channels/102860784329052160/565213527673929729/934812849635733605