I'm in the process of creating an application that utilizes the Gmail API. To send a reply to a thread, I must retrieve the Message-ID
and References
headers from the original message. These headers will then be included in the reply message. Despite successfully fetching the headers using the Gmail API, my code proceeds to send the reply without waiting for the fetch operation to complete. I have attempted to use promises, but being new to angularJS, I believe I may have not implemented them correctly. Any guidance on how I can improve my code would be greatly appreciated. Thank you.
public getReplyMessage(userId, messageId):Promise<any> {
var headersToReturn = {
'MessageID' : '',
'References' : '',
}
gapi.client.load('gmail', 'v1', () => {
var request = gapi.client.gmail.users.messages.get({
'userId': userId,
'id': messageId,
'format': 'metadata',
'metadataHeaders': [ 'Subject','References','Message-ID' ]
});
request.execute((message) => {
var headers = message.payload.headers;
$.each(headers, ( name , value ) => {
if(name == 'Message-ID'){
headersToReturn.MessageID = value;
}
else if(name == 'References'){
headersToReturn.References = value;
}
});
});
});
return Promise.resolve(headersToReturn);
}
Below is the code used to call the function.
this.gmailApiService.getReplyMessage('me', this.MsgId).then((msgHeadersForReply) => {
this.MessageIDHeader = msgHeadersForReply.MessageID;
this.ReferencesHeader = msgHeadersForReply.References;
});
console.log("MsgIDHeader => "+this.MessageIDHeader); // this logs empty string.
Any assistance provided will be highly valued. Thanks :)