Skip to content

Commit 4dcc5a9

Browse files
authored
fix(storage): use backward compatible return type in download function (#1750)
1 parent f84e3a7 commit 4dcc5a9

File tree

1 file changed

+22
-2
lines changed

1 file changed

+22
-2
lines changed

packages/core/storage-js/src/packages/BlobDownloadBuilder.ts

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@ import { isStorageError } from '../lib/errors'
22
import { DownloadResult } from '../lib/types'
33
import StreamDownloadBuilder from './StreamDownloadBuilder'
44

5-
export default class BlobDownloadBuilder implements PromiseLike<DownloadResult<Blob>> {
5+
export default class BlobDownloadBuilder implements Promise<DownloadResult<Blob>> {
6+
readonly [Symbol.toStringTag]: string = 'BlobDownloadBuilder'
7+
private promise: Promise<DownloadResult<Blob>> | null = null
8+
69
constructor(
710
private downloadFn: () => Promise<Response>,
811
private shouldThrowOnError: boolean
@@ -16,7 +19,24 @@ export default class BlobDownloadBuilder implements PromiseLike<DownloadResult<B
1619
onfulfilled?: ((value: DownloadResult<Blob>) => TResult1 | PromiseLike<TResult1>) | null,
1720
onrejected?: ((reason: any) => TResult2 | PromiseLike<TResult2>) | null
1821
): Promise<TResult1 | TResult2> {
19-
return this.execute().then(onfulfilled, onrejected)
22+
return this.getPromise().then(onfulfilled, onrejected)
23+
}
24+
25+
catch<TResult = never>(
26+
onrejected?: ((reason: any) => TResult | PromiseLike<TResult>) | null
27+
): Promise<DownloadResult<Blob> | TResult> {
28+
return this.getPromise().catch(onrejected)
29+
}
30+
31+
finally(onfinally?: (() => void) | null): Promise<DownloadResult<Blob>> {
32+
return this.getPromise().finally(onfinally)
33+
}
34+
35+
private getPromise(): Promise<DownloadResult<Blob>> {
36+
if (!this.promise) {
37+
this.promise = this.execute()
38+
}
39+
return this.promise
2040
}
2141

2242
private async execute(): Promise<DownloadResult<Blob>> {

0 commit comments

Comments
 (0)