我正试图使我的头脑在承诺,在这种情况下,在一个循环。
我的方案是基于上传文件到谷歌驱动器。我的理解是,每个文件都应该上传,然后一旦承诺被解决,上传下一个等等。
目前,我有一个函数可以上传一个文件并在完成后返回一个承诺:
# upload.js
const google = require('googleapis');
const drive = google.drive('v3');
function uploadFile(jwtClient, fileMetadata, media) {
return new Promise((resolve, reject) => {
drive.files.create({
auth: jwtClient,
resource: fileMetadata,
media,
fields: 'id, modifiedTime, originalFilename'
}, (err, uploadedFile) => {
if (err) reject(err);
// Promise is resolved with the result of create call
console.log("File Uploaded: " + uploadedFile.data.originalFilename);
resolve(uploadedFile)
});
});
}
module.exports = uploadFile;然后我想在一个循环中使用这个函数,我在这里的想法是,在uploadFile函数返回承诺之前,循环的下一个迭代不应该发生。
const google = require('googleapis');
const uploadFile = require('./components/upload');
const config = require('./gamechanger-creds.json');
const drive = google.drive('v3');
const targetFolderId = "1234"
var excel_files_array = [array if file names];
const jwtClient = new google.auth.JWT(
config.client_email,
null,
config.private_key,
['https://www.googleapis.com/auth/drive'],
null
);
jwtClient.authorize((authErr) => {
if (authErr) {
console.log(authErr);
return;
}
for(var i = 0; i < excel_files_array.length; i++) {
console.log("File Name is: " + excel_files_array[i]);
const fileMetadata = {
name: excel_files_array[i],
parents: [targetFolderId]
};
const media = {
mimeType: 'application/vnd.ms-excel',
body: fs.createReadStream('path/to/folder' + excel_files_array[i] )
};
uploadFile(jwtClient, fileMetadata, media);
}
});当运行这个时,我的输出如下
File Name is: arsenal_away.xlsx
File Name is: bournemouth_away.xlsx
File Name is: brighton_away.xlsx
File Name is: burnley_away.xlsx
File Name is: chelsea_away.xlsx
File Name is: crystal_palace_away.xlsx
File Name is: everton_away.xlsx
File Uploaded: undefined
(node:83552) UnhandledPromiseRejectionWarning: Unhandled promise rejection
(rejection id: 7): Error: Invalid multipart request with 0 mime parts.
(node:83552) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
File Uploaded: undefined
(node:83552) UnhandledPromiseRejectionWarning: Unhandled promise rejection
(rejection id: 9): Error: Invalid multipart request with 0 mime parts.
File Uploaded: undefined
File Uploaded: bournemouth_away.xlsx
File Uploaded: everton_away.xlsx
File Uploaded: burnley_away.xlsx
File Uploaded: arsenal_away.xlsx
File Uploaded: brighton_away.xlsx
File Uploaded: chelsea_away.xlsx
File Uploaded: crystal_palace_away.xlsx所以文件上传没有按顺序进行(不确定它们是否应该上传?)可能不是因为这一切都是异步发生的)。
我希望学习如何确保这些上传按照顺序(如果这确实是最好的方式),并确保文件上传解决了承诺,然后再转到下一个文件。
我也希望我能把我的剧本的第四部分包装成一个承诺,到目前为止还没有成功。
发布于 2018-02-13 11:23:38
只要把async/await放在它们所属的地方
async function uploadFile(jwtClient, fileMetadata, media) ...
async function uploadManyFiles(...)
for(....)
await uploadFile(...)这确保上传按顺序执行。如果您希望它们并行进行,那么将承诺分组到.all中
await Promise.all(files.map(uploadFile))对于auth,它与上传完全一样:
async function auth(...)
return new Promise((resolve, reject) => {
jwtClient = ...
jwtClient.authorize((authErr) => {
if (authErr) {
reject(authErr);
else
resolve(whatever)https://stackoverflow.com/questions/48765590
复制相似问题