Gmail’s app script API call for getting a thread’s messages takes particularly long (timing captured below), so I’m looking for a way to asynchronously kick off each call in the page_count
batch, and then wait for the results of all the .getMessages()
calls at the end. Unfortunately, .getMessages()
is not natively asynchronous, so how can I make this work?
for (var page_i = 0; page_i < unread_count; page_i += page_count) {
Logger.log("Page count = " + page_i);
var inbox_threads = GmailApp.getInboxThreads(page_i, page_count);
// var inbox_threads = GmailApp.search('in:inbox', page_i, page_count);
Logger.log("Got threads, getting messages...");
for (var i = 0; i < inbox_threads.length; i++) {
var message = inbox_threads[i].getMessages();
var sender = message[0].getFrom();
if (sender in cObj) {
cObj[sender]++;
} else {
cObj[sender] = 1;
}
}
}
Execution log:
3:51:44 AM Info Page count = 0
3:51:45 AM Info Got threads, getting messages...
3:52:17 AM Info Page count = 250
3:52:17 AM Info Got threads, getting messages...
3:52:50 AM Info Page count = 500
3:52:51 AM Info Got threads, getting messages...
3:53:23 AM Info Page count = 750