I am making multiple ajax calls using promises how do I return to the previous function by setting a flag. I have a save function in which I am calling a method if(!x()){return;}
I want to return some flag out of the x function I used promises for making the ajax calls as I need to wait for multiple ajax request responses and depending on it I have to submit the form. Please check the fiddleFiddle
Asked
Active
Viewed 120 times
0

Sweety
- 31
- 6
-
3It sounds like you're trying to return a value from an asynchronous function to the code that called it. That's impossible. (the fiddle is useless) – Kevin B Jan 18 '18 at 21:35
-
The test `if (!this.refValid()) { ... }` proves @KevinB to be right. `refValid()` does a bunch of asynchronous stuff and should return a promise (which it can do with no need for a Deferred). This question is a rather extensive example of something that is answered by https://stackoverflow.com/questions/14220321/how-do-i-return-the-response-from-an-asynchronous-call – Roamer-1888 Jan 19 '18 at 00:23
1 Answers
0
This kind of thing is a nightmare unless you are familiar with Promises.
As far as I can tell, you want something like this :
Ext.define('MyApp.view.MyModel1FormViewController', {
'extend': 'Ext.app.ViewController',
'alias': 'controller.mymodel1form',
'save': function (button, e, eOpts, processed) {
var me = this;
// enable/disable "submit" button based on Promise-wrapped result from refValid()
return this.refValid().then(function(bool) {
// success handler
me.onSubmit(button, e, eOpts, bool);
Ext.Msg.alert('save: success');
}, function (e) {
// error handler
// me.onSubmit(button, e, eOpts, ???); true|false or maybe just leave the button in its current state?
Ext.Msg.alert('save: errors');
// throw e; // rethrow the error to inform save()'s caller.
});
},
'refValid': function (button, e, eOpts) {
var detailStore = this.getView().up('panel').lookupReference('list').getStore(),
jde = this.getView().up('panel').lookupReference('headerform').lookupReference('jdeno').getValue();
var promises = detailStore.data.items.map(function (r) { // ???
return r.get('refno') || null;
}).filter(function (x) {
return !!x; // filter out any nulls
}).map(function (customerInvoiceDebitNo) {
return Ext.Ajax.request({
'url': 'data1.json',
'timeout': 120000,
'method': 'GET',
'params': {
'addressNumber': jde,
'documentNumber': customerInvoiceDebitNo
},
'customerInvoiceDebitNo': customerInvoiceDebitNo
}).then(function (response) {
if (Ext.decode(response.responseText).data.length === 0) {
return false; // null
} else {
Ext.Array.remove(customerInvoiceDebitNo); // maybe not necessary?
return true; // not null
}
});
});
return Ext.Promise.all(promises).then(function (results) { // results is array of booleans
// Scan the results to check whether or not all are `true`,
// and return <boolean>.
return results.reduce(function (prev, current) {
return prev && current;
}, true);
});
}
});

Roamer-1888
- 19,138
- 5
- 33
- 44
-
Thanks Roamer! I tried putting your code in the fiddle but got some errors can you please take a look at this https://fiddle.sencha.com/#view/editor&fiddle/2c4e – Sweety Jan 19 '18 at 14:03
-
Answer edited. It runs now all the way through to "Save: success". I don't how to test whether a "save" actually happens. – Roamer-1888 Jan 20 '18 at 02:21