问题
Google recommends using promises, but its code examples do not, and I'm struggling to make the gmail api work with promises when I modify the code from the online docs.
All I've changed are the lines below, but I get an error
VM677:5 Uncaught TypeError: gmail.users.messages.list(...).then is not a function
gmail.users.messages.list({
auth: auth,
userId: 'me',
labelIds: 'Label_14'
// }, function(err, response) {
// if (err) {
// console.log('The API returned an error: ' + err);
// return;
// }
// console.log(response);
})
.then(response => {
console.log("success", response);
})
Most of the examples of SO use promises so I think it should be possible but I can't see what the problem is. Would really welcome some help
回答1:
The googleapis
module does not support promises.
Consider using util.promisify if you want to use promises with this module.
var list = util.promisify(gmail.users.messages.list);
list({
auth: auth,
userId: 'me',
labelIds: 'Label_14'
})
.then(...);
来源:https://stackoverflow.com/questions/47400116/nodejs-gmail-api-not-supporting-promises