There is no way to to make all code synchronous, you can use callbacks
or promises/await
to wait for the response.
Example with callback:
{
send: function (e) {
e.preventDefault();
var self = this;
var is_submit = self.$target.find('#is_submit').val();
var mobile = self.$target.find('#mobile').val();
var phone = self.$target.find('#phone').val();
var data = self.ajaxcall(mobile, function(result){
console.log('the result is: ', result)
self._super(e);
});
},
ajaxcall: function (mobile, callback) {
var value = {
'flag': 'mobile',
'number': mobile
}
ajax.jsonRpc('/checkexisting/', 'call', value).then(function (data) {
console.log('isnide ajax call', data);
callback(data);
});
}
Example with promises:
{
send: function (e) {
e.preventDefault();
var self = this;
var is_submit = self.$target.find('#is_submit').val();
var mobile = self.$target.find('#mobile').val();
var phone = self.$target.find('#phone').val();
var data = self.ajaxcall(mobile).then(function(result){
console.log('the result is: ', result)
self._super(e);
});
},
ajaxcall: function (mobile) {
var value = {
'flag': 'mobile',
'number': mobile
}
return new Promise(function(res, rej){
ajax.jsonRpc('/checkexisting/', 'call', value).then(function (data) {
console.log('isnide ajax call', data);
res(data);
})
});
}
Or with await(note the async
and await
keywords):
{
send: async function (e) {
e.preventDefault();
var self = this;
var is_submit = self.$target.find('#is_submit').val();
var mobile = self.$target.find('#mobile').val();
var phone = self.$target.find('#phone').val();
var data = await self.ajaxcall(mobile);
console.log(data)
self._super(e);
},
ajaxcall: function (mobile) {
var value = {
'flag': 'mobile',
'number': mobile
}
return new Promise(function(res, rej){
ajax.jsonRpc('/checkexisting/', 'call', value).then(function (data) {
console.log('isnide ajax call', data);
res(data);
})
});
}