Menu
Forums
All threads
Latest threads
New posts
Trending threads
New posts
Search forums
Trending
What's new
New posts
New profile posts
Latest activity
Members
Current visitors
New profile posts
Search profile posts
Upgrades
Log in
Register
What's new
Search
Search
Search titles only
By:
All threads
Latest threads
New posts
Trending threads
New posts
Search forums
Menu
Log in
Register
Navigation
Install the app
Install
More options
Contact us
Close Menu
Forums
Software Development
Programming
Programming Q&A
JavaScript API - Sync Method
JavaScript is disabled. For a better experience, please enable JavaScript in your browser before proceeding.
You are using an out of date browser. It may not display this or other websites correctly.
You should upgrade or use an
alternative browser
.
Reply to thread
Message
<blockquote data-quote="JayC" data-source="post: 466013" data-attributes="member: 36373"><p>Hey Guys,</p><p></p><p>How can I call a sync function for ajax url GET, and wait for a result (rather than returning "undefined" or an unfilled PROMISE ?</p><p></p><p>I have tried so many things and I can't get this function to work properly.</p><p></p><p>Parent Function:</p><p>[CODE]checkValidity: function(input) {</p><p> for ( var i = 0; i < this.validityChecks.length; i++ ) {</p><p></p><p> var isInvalid = this.validityChecks[i].isInvalid(input);</p><p> if (isInvalid) {</p><p> this.addInvalidity(this.validityChecks[i].invalidityMessage);</p><p> }</p><p></p><p> var requirementElement = this.validityChecks[i].element;</p><p></p><p> if (requirementElement) {</p><p> if (isInvalid) {</p><p> requirementElement.classList.add('invalid');</p><p> requirementElement.classList.remove('valid');</p><p> } else {</p><p> requirementElement.classList.remove('invalid');</p><p> requirementElement.classList.add('valid');</p><p> }</p><p></p><p> } // end if requirementElement</p><p> } // end for</p><p> }[/CODE]</p><p></p><p>The line that calls this is the:</p><p></p><p>[CODE]this.validityChecks[i].isInvalid(input)[/CODE]</p><p></p><p>Which references this predefined block:</p><p>[CODE]var phoneValidityChecks = [</p><p> {</p><p> isInvalid: function(input) {</p><p> const regex = RegExp(/\d{10}$/);</p><p> return !regex.test(input.value);</p><p> },</p><p> invalidityMessage: 'Please enter a valid phone number',</p><p> element: document.querySelector('label[for="register_phone"] .input-requirements li:nth-child(1)')</p><p> },</p><p> {</p><p> isInvalid: function(input) {</p><p> checkPhoneExists(input.value).then(json => {</p><p> if (!Array.isArray(json['data']) || json['data'].length === 0)</p><p> return true;</p><p> </p><p> var exists = json['data'][0];</p><p></p><p> return exists;</p><p> });</p><p> },</p><p> invalidityMessage: 'Please enter a unique phone number',</p><p> element: document.querySelector('label[for="register_phone"] .input-requirements li:nth-child(2)')</p><p> }</p><p>];[/CODE]</p><p></p><p>So I am trying to call this function "checkPhoneExists" which will do the following:</p><p>[CODE]async function checkPhoneExists(phone) {</p><p> $.when(checkPhoneNumberAjax(phone)).done(function(result){</p><p> return result;</p><p> });</p><p>}[/CODE]</p><p></p><p>And the Ajax Function:</p><p></p><p>[CODE]async function checkPhoneNumberAjax(phone) {</p><p> var username = 'User';</p><p> var password = 'Pass';</p><p> var url = 'http://127.0.0.1/app/api/api.php';</p><p> </p><p> var postData = {</p><p> "type" : 'GET',</p><p> "action" : 'phone_exists',</p><p> "phone" : phone,</p><p> };</p><p> </p><p> $.ajax({</p><p> url: url,</p><p> type: 'GET',</p><p> dataType: 'json',</p><p> data: postData,</p><p> contentType: 'application/json',</p><p> beforeSend: function(xhr) {</p><p> xhr.setRequestHeader("Authorization", "Basic "+btoa(username+':'+password));</p><p> },</p><p> success: function(resultData){</p><p> return resultData;</p><p> }</p><p> });</p><p> </p><p> await new Promise(resolve => setTimeout(resolve, 3000));</p><p> </p><p> return true;</p><p>}[/CODE]</p><p></p><p>I have tried everything I can think of to STOP RETURNING and WAIT FOR A RESULT!</p></blockquote><p></p>
[QUOTE="JayC, post: 466013, member: 36373"] Hey Guys, How can I call a sync function for ajax url GET, and wait for a result (rather than returning "undefined" or an unfilled PROMISE ? I have tried so many things and I can't get this function to work properly. Parent Function: [CODE]checkValidity: function(input) { for ( var i = 0; i < this.validityChecks.length; i++ ) { var isInvalid = this.validityChecks[i].isInvalid(input); if (isInvalid) { this.addInvalidity(this.validityChecks[i].invalidityMessage); } var requirementElement = this.validityChecks[i].element; if (requirementElement) { if (isInvalid) { requirementElement.classList.add('invalid'); requirementElement.classList.remove('valid'); } else { requirementElement.classList.remove('invalid'); requirementElement.classList.add('valid'); } } // end if requirementElement } // end for }[/CODE] The line that calls this is the: [CODE]this.validityChecks[i].isInvalid(input)[/CODE] Which references this predefined block: [CODE]var phoneValidityChecks = [ { isInvalid: function(input) { const regex = RegExp(/\d{10}$/); return !regex.test(input.value); }, invalidityMessage: 'Please enter a valid phone number', element: document.querySelector('label[for="register_phone"] .input-requirements li:nth-child(1)') }, { isInvalid: function(input) { checkPhoneExists(input.value).then(json => { if (!Array.isArray(json['data']) || json['data'].length === 0) return true; var exists = json['data'][0]; return exists; }); }, invalidityMessage: 'Please enter a unique phone number', element: document.querySelector('label[for="register_phone"] .input-requirements li:nth-child(2)') } ];[/CODE] So I am trying to call this function "checkPhoneExists" which will do the following: [CODE]async function checkPhoneExists(phone) { $.when(checkPhoneNumberAjax(phone)).done(function(result){ return result; }); }[/CODE] And the Ajax Function: [CODE]async function checkPhoneNumberAjax(phone) { var username = 'User'; var password = 'Pass'; var url = 'http://127.0.0.1/app/api/api.php'; var postData = { "type" : 'GET', "action" : 'phone_exists', "phone" : phone, }; $.ajax({ url: url, type: 'GET', dataType: 'json', data: postData, contentType: 'application/json', beforeSend: function(xhr) { xhr.setRequestHeader("Authorization", "Basic "+btoa(username+':'+password)); }, success: function(resultData){ return resultData; } }); await new Promise(resolve => setTimeout(resolve, 3000)); return true; }[/CODE] I have tried everything I can think of to STOP RETURNING and WAIT FOR A RESULT! [/QUOTE]
Insert quotes…
Verification
Post reply
Forums
Software Development
Programming
Programming Q&A
JavaScript API - Sync Method
Top