Articles

Affichage des articles du 2023

JavaScript - fetch POST request with async stored result

fetch POST request with async stored result function myFunc (){ if (GLOBAL. data === null ){ let formData = new FormData (); formData . append ( "filters" , JSON . stringify (GLOBAL. filters )); fetch ( "myScript.php" , { method : "POST" , body : formData }) . then ((response) => { if (response. ok ) { return response. json (); } else { alert ( `Error: ${response. statusText } ` ); } }) . then (data => { GLOBAL. data = data; }) . catch ( function (error) { console . error ( "fetch error: " + error. message ); }); } ( async () => { while (GLOBAL. data === null ) await new Promise (resolve => setTimeout (resolve, 1000 )); /*           *  here some code wich can use GLOBAL. data sto