Angular,承诺:在批处理完成之前不要 return 承诺
Angular, Promises: Don't return promise till batch has completed
TL;DR 我必须从 S3 读取 3 个文件。我正在使用 Promises,我不希望它们 "return" 直到它们全部 returned
我有用于在 Angular 设置中从 S3 读取的代码。它看起来有点像这样:
return new Promise((resolve, reject) => {
const s3 = new AWS.S3({
apiVersion: '2006-03-01',
params: {
Bucket: this._bucketKey,
Key: this._someKey
}
});
s3.config.update({credentials: AWS.config.credentials});
s3.getObject((err, data) => {
...
}
});
});
}
我想做的是将 S3 键列表传递给该函数,然后在所有 3 次调用的数据之后将其 return,因为此读取包含在一个函数中。
readMultipleKeys(s3Keys:[]): Promise<any> {
data.forEach( value => {
"read value key from S3"
});
// resolve after all the array items in data have been requested and returned
}
非常感谢任何帮助
假设您有一个名为“getFileFromAws()”的方法,它接收 S3key 并执行 http 调用以从 S3 存储中获取文件。您预期的解决方案如下所示。
getFileFromAws(s3Key) {
// Perform the HTTP call to get the file like below
// return this.http.get(url, httpOptions).toPromise();
}
async readMultipleKeys(s3Keys:[]) {
let files = await Promise.all(s3Keys.map(key => getFileFromAws(key)));
console.log(files);
// now the files array contains 3 files if you passed 3 keys to this method
// Perform your logic
}
我创建了一个 demo Stackblitz app 来展示 Promise.all 方法。
TL;DR 我必须从 S3 读取 3 个文件。我正在使用 Promises,我不希望它们 "return" 直到它们全部 returned
我有用于在 Angular 设置中从 S3 读取的代码。它看起来有点像这样:
return new Promise((resolve, reject) => {
const s3 = new AWS.S3({
apiVersion: '2006-03-01',
params: {
Bucket: this._bucketKey,
Key: this._someKey
}
});
s3.config.update({credentials: AWS.config.credentials});
s3.getObject((err, data) => {
...
}
});
});
}
我想做的是将 S3 键列表传递给该函数,然后在所有 3 次调用的数据之后将其 return,因为此读取包含在一个函数中。
readMultipleKeys(s3Keys:[]): Promise<any> {
data.forEach( value => {
"read value key from S3"
});
// resolve after all the array items in data have been requested and returned
}
非常感谢任何帮助
假设您有一个名为“getFileFromAws()”的方法,它接收 S3key 并执行 http 调用以从 S3 存储中获取文件。您预期的解决方案如下所示。
getFileFromAws(s3Key) {
// Perform the HTTP call to get the file like below
// return this.http.get(url, httpOptions).toPromise();
}
async readMultipleKeys(s3Keys:[]) {
let files = await Promise.all(s3Keys.map(key => getFileFromAws(key)));
console.log(files);
// now the files array contains 3 files if you passed 3 keys to this method
// Perform your logic
}
我创建了一个 demo Stackblitz app 来展示 Promise.all 方法。