From 4b035e49da46f6d7051670c69632a67334bf00aa Mon Sep 17 00:00:00 2001 From: Loule | Louis <35641311+Loule95450@users.noreply.github.com> Date: Tue, 27 Feb 2024 16:40:32 +0100 Subject: [PATCH] Update src/service/apiCall.ts Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com> --- src/service/apiCall.ts | 33 ++++++++++++++++----------------- 1 file changed, 16 insertions(+), 17 deletions(-) diff --git a/src/service/apiCall.ts b/src/service/apiCall.ts index 0ee9b40b..cb40d0e1 100644 --- a/src/service/apiCall.ts +++ b/src/service/apiCall.ts @@ -10,23 +10,22 @@ export async function getProduct(barcode: string) { } let data: ProductData | null = null; - await fetch(apiUrl + barcode) - .then(result => result.json()) - .then(json => { - data.code = json.code; - data.name = json.product.generic_name_fr - ? json.product.generic_name_fr - : json.product.generic_name_en - ? json.product.generic_name_en - : json.product.generic_name; - data.nutriscore = json.product.nutriscore_grade; - data.categories = json.product.categories; - data.nutriments = json.product.nutriments; - }) - .catch(error => { - console.error(error); - data = null; - }); + try { + const response = await fetch(apiUrl + barcode); + const json = await response.json(); + data.code = json.code; + data.name = json.product.generic_name_fr + ? json.product.generic_name_fr + : json.product.generic_name_en + ? json.product.generic_name_en + : json.product.generic_name; + data.nutriscore = json.product.nutriscore_grade; + data.categories = json.product.categories; + data.nutriments = json.product.nutriments; + } catch (error) { + console.error(error); + data = null; + } return data; }