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

typescript react-native : async function return res to late

$
0
0

I trying to search on database some information, and update my state after get the response.

for example :

export const barcodeOnitems = async (barcode: string) => {    db.transaction((tx) => {        tx.executeSql('SELECT * FROM items where barcode = ?',            [barcode],            (txObj, resultSet) => {                if(resultSet.rows.length){                    return true;                }else{                    return false;                }            },            (err) => {                console.log(err);                return false;            }        );    });};
import { barcodeOnitems } from '../tools/database'[...]const [result, setResult] = useState(null);[...]barcodeOnitems(val).then(async (res) => {    setResult(res)});

But I'm almost sure it's because setResult(res) work before the return of barcodeOnitems.

So I try to use async function but I don't understand why this doesn't work.

Thanks for answers

EDIT :what about promise ?

if(resultSet.rows.length){    return new Promise((resolve) => {        resolve(true)    });    }else{        return new Promise((resolve) => {            resolve(false)    });}

Viewing all articles
Browse latest Browse all 6214

Trending Articles



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