Quantcast
Channel: Active questions tagged react-native+typescript - Stack Overflow
Viewing all articles
Browse latest Browse all 6287

React Native - Assign value from promise to variable

$
0
0

I have a couple repositories containing database calls such as getUser and getUsers.Now I export those repositories as followed:

import { DatabaseBackend } from "../common/database";import {    userRepository as _userRepository,    bookingRepository as _bookingRepository,    pricingRepository as _pricingRepository,} from "../common/repositories";const backend = DatabaseBackend.ReactNative;export const userRepository = _userRepository(backend);export const bookingRepository = _bookingRepository(backend);export const pricingRepository = _pricingRepository(backend);

This leads to me having to do things like this:

const fetchUserData = (uid: string) => {        userRepository            .then((u) => {                u.getUser(uid)                    .then((user: User) => {                        if(!user) {                            return;                        }                        setUser(user);                    })                    .catch((err) => {                        logger.error("Something went wrong while fetching user information", err);                    });            })            .catch(err => {                logger.error("Something went wrong while fetching user information", err);            });    };

The intellisense says userRepository is:

const userRepository: Promise<{    getUser: (id: string) => Promise<User>;    getNanny: (id: string) => Promise<Nanny>;    getNannies: () => Promise<Nanny[]>;    getParent: (id: string) => Promise<Parent>;    updateUser: (user: User) => Promise<void>;}>

Is there a way how to assign the functions to userRepository without me having to do any then calls on it? I'd like to just being able to call userRepository.getUser() for example.


Viewing all articles
Browse latest Browse all 6287

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>