Ecmascript 6 从ES2018异步/等待到ES2015承诺。。。超时
我正在尝试将ES2018Ecmascript 6 从ES2018异步/等待到ES2015承诺。。。超时,ecmascript-6,promise,async-await,Ecmascript 6,Promise,Async Await,我正在尝试将ES2018async函数转换为ES2015(ES6)函数,但是我得到了一个超时,我猜我的ES2015版本是错误的…但是在哪里 ES2018版本 async function connectGoogleAPI () { // Create a new JWT client using the key file downloaded from the Google Developer Console const client = await google
async
函数转换为ES2015(ES6)函数,但是我得到了一个超时,我猜我的ES2015版本是错误的…但是在哪里
ES2018版本
async function connectGoogleAPI () {
// Create a new JWT client using the key file downloaded from the Google Developer Console
const client = await google.auth.getClient({
keyFile: path.join(__dirname, 'service-key.json'),
scopes: 'https://www.googleapis.com/auth/drive.readonly'
});
// Obtain a new drive client, making sure you pass along the auth client
const drive = google.drive({
version: 'v2',
auth: client
});
// Make an authorized request to list Drive files.
const res = await drive.files.list();
console.log(res.data);
return res.data;
}
ES2015版w/Promise
function connectGoogleAPI () {
return new Promise((resolve, reject) => {
const authClient = google.auth.getClient({
keyFile: path.join(__dirname, 'service-key.json'),
scopes: 'https://www.googleapis.com/auth/drive.readonly'
});
google.drive({
version: 'v2',
auth: authClient
}), (err, response) => {
if(err) {
reject(err);
} else {
resolve(response);
}
}
});
}
您尚未翻译
getClient
的wait
。记住,等待
=然后
(大致)。你也会陷入这样的困境:当你已经有了承诺(来自getClient
),你几乎不需要使用新承诺。只需使用然后
下面是一个示例,每个wait
转换为然后
,使用链进行后续操作:
function connectGoogleAPI () {
// Create a new JWT client using the key file downloaded from the Google Developer Console
return google.auth.getClient({
keyFile: path.join(__dirname, 'service-key.json'),
scopes: 'https://www.googleapis.com/auth/drive.readonly'
}).then(client => {
// Obtain a new drive client, making sure you pass along the auth client
const drive = google.drive({
version: 'v2',
auth: client
});
// Make an authorized request to list Drive files.
return drive.files.list();
}).then(res => {
console.log(res.data);
return res.data;
});
}
最后一部分可以是
}).then(res => res.data);
…如果您删除控制台.log
。(或者我们可以滥用逗号运算符。)
注:
- 每个
wait
都需要成为然后处理程序(原始处理程序中有两个,waitinggetClient
和drive.files.list
)
- 在
then
处理程序中,如果您必须等待另一个承诺(例如来自drive.files.list
)的承诺,您通常会从处理程序返回该承诺,然后使用另一个处理程序来处理该结果(这就是为什么我使用return drive.files.list()
然后是一个单独的处理程序,用于将res
转换为res.data
)
关于第二点:有时嵌套是合适的,例如当您需要将结果与只在then
处理程序中具有的某个中间值相结合时。(例如,如果我们想将res.data
与client
相结合,但通常不想嵌套。您还没有翻译getClient
的wait
。记住,等待
=然后
(大致)。你也会陷入这样的困境:当你已经有了承诺(来自getClient
),你几乎不需要使用新承诺。只需使用然后
下面是一个示例,每个wait
转换为然后
,使用链进行后续操作:
function connectGoogleAPI () {
// Create a new JWT client using the key file downloaded from the Google Developer Console
return google.auth.getClient({
keyFile: path.join(__dirname, 'service-key.json'),
scopes: 'https://www.googleapis.com/auth/drive.readonly'
}).then(client => {
// Obtain a new drive client, making sure you pass along the auth client
const drive = google.drive({
version: 'v2',
auth: client
});
// Make an authorized request to list Drive files.
return drive.files.list();
}).then(res => {
console.log(res.data);
return res.data;
});
}
最后一部分可以是
}).then(res => res.data);
…如果您删除控制台.log
。(或者我们可以滥用逗号运算符。)
注:
- 每个
wait
都需要成为然后处理程序(原始处理程序中有两个,waitinggetClient
和drive.files.list
)
- 在
then
处理程序中,如果您必须等待另一个承诺(例如来自drive.files.list
)的承诺,您通常会从处理程序返回该承诺,然后使用另一个处理程序来处理该结果(这就是为什么我使用return drive.files.list()
然后是一个单独的处理程序,用于将res
转换为res.data
)
关于第二点:有时嵌套是合适的,例如当您需要将结果与只在then
处理程序中具有的某个中间值相结合时。(例如,如果我们想将res.data
与client
相结合,但通常不希望嵌套。。。。但是Promise
是ES6的标准功能,所以在ES5版本中,您实际上使用了一个库?而async
是ES7+,而不是ES6,你的意思是ES6对ES7吗?你的版本错了。:-)承诺是ES2015(也称为“ES6”)async
/await
是ES2018。您使用的google.auth.getClient()
库是什么?在您的ES6版本中,您正在await
google.auth.getClient()
但在ES5版本中,您没有google.auth.getClient()
的回调(我已经为您修复了这些版本)。。。但是Promise
是ES6的标准功能,所以在ES5版本中,您实际上使用了一个库?而async
是ES7+,而不是ES6,你的意思是ES6对ES7吗?你的版本错了。:-)承诺是ES2015(也称为“ES6”)async
/await
是ES2018。您使用的google.auth.getClient()
库是什么?在您的ES6版本中,您正在await
google.auth.getClient()
但在ES5版本中,您没有google.auth.getClient()
的回调(我已经为您修复了这些版本)非常感谢您的反馈。。。现在开始测试它并运行!太酷了。。。此函数在Firebase函数内执行,使用默认的Firebase项目应用程序引擎服务帐户(w角色帐户令牌创建者)。此服务帐户被授权在给定范围内访问Google Drive API。。。嗯,我非常感谢你的反馈。。。现在开始测试它并运行!太酷了。。。此函数在Firebase函数内执行,使用默认的Firebase项目应用程序引擎服务帐户(w角色帐户令牌创建者)。此服务帐户被授权在给定范围内访问Google Drive API。。。做得好