Skip to Content
0

Processing a loop with promises.

Oct 17, 2017 at 10:02 AM

57

avatar image
oModel.create("/SurveySet", oEntry, { 
    success: function(oData) { 
        for (var i = 0; i < questionData.questions.length; i++) {
            var oEntry = {};
            oEntry.SurveyId = oData.SurveyId; 

            oModel.create("/QuestionSet", oEntry, { 
                changeSetId: i.toString(), 
                success: function(oData) {
                    //Additional Processing

                }
           }
        }
    }
}

I'm Creating a SurveySet which returns a SurveyId.

For each Survey I need to create a QuestionSet in a FOR loop, returning the Data each time for additional processing.

The issue is in the order of execution. The for loop is incremented BEFORE the oData is retrieved from the 1st iteration.

E.g. if the for loop has 0 and 1, only the last element 1 is executed.

How do I delay the incrementation of the for loop until AFTER the oData is returned for the first iteratiion of the loop?

10 |10000 characters needed characters left characters exceeded
* Please Login or Register to Answer, Follow or Comment.

2 Answers

Best Answer
Daniel Degraf
Oct 18, 2017 at 11:33 AM
0

Hi,

I am not 100% sure what you want to do, but a few options that came to my mind where either using synchronous calls (instead of asynchronous calls you use in your example) or using a recursive approach. Something like:

oModel.create("/SurveySet", oEntry, { 
    success: function(oData) { 
		var count = 0;
		function recursion() {
			if (count < questionData.questions.length) {
				oModel.create("/QuestionSet", oEntry, { 
                changeSetId: i.toString(), 
                success: function(oData) {
                    //Additional Processing here
					count++;
					recursion();
                }
				}
			} else {
				return;
			}
		}
		recursion();
    }
}

Maybe these ideas lead to your desired solution. Please keep in mind to have a nice user experience in the end (if the user has to wait couple of seconds until he can proceed working, the user might get annoyed). Maybe you can also adapt your service to accept several Questions at once or so. You might try to work with Batch Processing (something like this: https://blogs.sap.com/2015/03/06/simple-steps-to-perform-batch-operations-in-sap-gateway-services/)

And you can try to work with ES6Promises, like in this example:

_onButtonPress1: function() {
			var oDialog = this.getView().getContent()[0];


			return new ES6Promise.Promise(function(fnResolve, fnReject) {
				oDialog.attachEventOnce("afterClose", null, fnResolve);
				oDialog.close();
			});


		}

I would be interested, what option you chose or how you've implemented it :-)

Cheers

Daniel

Share
10 |10000 characters needed characters left characters exceeded
Adam Harkus Oct 19, 2017 at 08:50 PM
0

The issue was not related to Promises in the end. The Gateway service was simply crashing out on the 2nd record and I didn't realise.

Once I'd fixed the error changsetId worked fine.

Share
10 |10000 characters needed characters left characters exceeded