@@ -1208,42 +1208,29 @@ export class DocumentReference<T = firestore.DocumentData>
1208
1208
get ( options ?: firestore . GetOptions ) : Promise < firestore . DocumentSnapshot < T > > {
1209
1209
validateBetweenNumberOfArgs ( 'DocumentReference.get' , arguments , 0 , 1 ) ;
1210
1210
validateGetOptions ( 'DocumentReference.get' , options ) ;
1211
- return new Promise (
1212
- ( resolve : Resolver < firestore . DocumentSnapshot < T > > , reject : Rejecter ) => {
1213
- if ( options && options . source === 'cache' ) {
1214
- this . firestore
1215
- . ensureClientConfigured ( )
1216
- . getDocumentFromLocalCache ( this . _key )
1217
- . then ( doc => {
1218
- resolve (
1219
- new DocumentSnapshot (
1220
- this . firestore ,
1221
- this . _key ,
1222
- doc ,
1223
- /*fromCache=*/ true ,
1224
- doc instanceof Document ? doc . hasLocalMutations : false ,
1225
- this . _converter
1226
- )
1227
- ) ;
1228
- } , reject ) ;
1229
- } else {
1230
- getDocViaSnapshotListener (
1231
- this . _firestoreClient ,
1232
- this ,
1233
- async snapshot => {
1234
- const viewSnapshot = await snapshot ;
1235
- resolve (
1236
- viewSnapshot
1237
- ? this . _convertToDocSnapshot ( viewSnapshot )
1238
- : undefined
1239
- ) ;
1240
- } ,
1241
- reject ,
1242
- options
1243
- ) ;
1244
- }
1245
- }
1246
- ) ;
1211
+
1212
+ if ( options && options . source === 'cache' ) {
1213
+ return this . firestore
1214
+ . ensureClientConfigured ( )
1215
+ . getDocumentFromLocalCache ( this . _key )
1216
+ . then (
1217
+ doc =>
1218
+ new DocumentSnapshot (
1219
+ this . firestore ,
1220
+ this . _key ,
1221
+ doc ,
1222
+ /*fromCache=*/ true ,
1223
+ doc instanceof Document ? doc . hasLocalMutations : false ,
1224
+ this . _converter
1225
+ )
1226
+ ) ;
1227
+ } else {
1228
+ return getDocViaSnapshotListener (
1229
+ this . _firestoreClient ,
1230
+ this ,
1231
+ options
1232
+ ) . then ( snapshot => this . _convertToDocSnapshot ( snapshot ) ) ;
1233
+ }
1247
1234
}
1248
1235
1249
1236
withConverter < U > (
@@ -1315,10 +1302,9 @@ function addDocSnapshotListener<T>(
1315
1302
export function getDocViaSnapshotListener < T > (
1316
1303
firestoreClient : FirestoreClient ,
1317
1304
ref : DocumentKeyReference < T > ,
1318
- resolve : Resolver < ViewSnapshot > ,
1319
- reject : Rejecter ,
1320
1305
options ?: firestore . GetOptions
1321
- ) : void {
1306
+ ) : Promise < ViewSnapshot > {
1307
+ const result = new Deferred < ViewSnapshot > ( ) ;
1322
1308
const unlisten = addDocSnapshotListener (
1323
1309
firestoreClient ,
1324
1310
ref ,
@@ -1341,7 +1327,7 @@ export function getDocViaSnapshotListener<T>(
1341
1327
// the server so we can deliver that even when you're
1342
1328
// offline 2) Actually reject the Promise in the online case
1343
1329
// if the document doesn't exist.
1344
- reject (
1330
+ result . reject (
1345
1331
new FirestoreError (
1346
1332
Code . UNAVAILABLE ,
1347
1333
'Failed to get document because the client is ' + 'offline.'
@@ -1353,7 +1339,7 @@ export function getDocViaSnapshotListener<T>(
1353
1339
options &&
1354
1340
options . source === 'server'
1355
1341
) {
1356
- reject (
1342
+ result . reject (
1357
1343
new FirestoreError (
1358
1344
Code . UNAVAILABLE ,
1359
1345
'Failed to get document from server. (However, this ' +
@@ -1363,12 +1349,13 @@ export function getDocViaSnapshotListener<T>(
1363
1349
)
1364
1350
) ;
1365
1351
} else {
1366
- resolve ( snap ) ;
1352
+ result . resolve ( snap ) ;
1367
1353
}
1368
1354
} ,
1369
- error : reject
1355
+ error : e => result . reject ( e )
1370
1356
}
1371
1357
) ;
1358
+ return result . promise ;
1372
1359
}
1373
1360
1374
1361
export class SnapshotMetadata implements firestore . SnapshotMetadata {
0 commit comments