nodejs gmail api not supporting promises

巧了我就是萌 提交于 2021-01-27 13:44:37

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!