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) });}