locked
Lightswitch html how to know when async iteration is done. RRS feed

  • Question

  • Hello i deed some help please.

    I have an async operation inside another async operation so how can i know when everything is done.

    This is my code

    var confirmedGirls = 0;

    msls.showProgress(msls.promiseOperation(function (operation) {
                screen.Staff.getConfirmedWaiters().then(function (result) {
                    result.each(function (item) {
                        item.getWaiter().then(function (result) {
                            if (result.Gender == "Female") {
                                confirmedGirls++;
                            }
                        });
                    });
                    operation.complete(confirmedGirls);
                });
            }).then(function (result) {

    ...

    So first i load the ConfirmedWaiters collection, and when it's complete i iterate every entity and load a child entity async, so i want to know when the iteration is complete but the problem is that its returning right away because its async so how can i wait till the iteration is complete and then execute the remaining code?

    Thanks






    • Edited by M. Lerner Thursday, February 26, 2015 4:28 PM
    Thursday, February 26, 2015 4:56 AM

All replies

  • so i want to know when the iteration is complete but the problem is that its returning right away because its async so how can i wait till the iteration is complete and then execute the remaining code?

    How about using await operation in your application till the iteration is complete.
    Tuesday, March 10, 2015 9:16 AM
  • In order to tackle this type of asynchronous challenge, you need to join your promises.

    The following blog provides some background and is a useful read for any LightSwitch HTML Client developer (it's always worth bearing in mind that LightSwitch HTML is largely WinJS based and any WinJS promise related material is worth reading): -

    All about promises (for Windows Store apps written in JavaScript)

    Based on this approach, your code should end up similar to the following which should achieve the desired result (though it may encounter a Gender Equality Exception ;-)

    msls.showProgress(msls.promiseOperation(function (operation) {
        screen.Staff.getConfirmedWaiters().then(function (result) {
            var ps = [];
            result.each(function (item) {
                var p = item.getWaiter().then(function (result) {
                    if (result.Gender == "Female") {
                        confirmedGirls++;
                    }
                });
                ps.push(p);
            });
            WinJS.Promise.join(ps).then(function onComplete(result) {
                operation.complete(confirmedGirls);
            }, function onError(error) {
                operation.error(error);
            });
        });
    }).then(function (result) {
    

    HTH,

    Chris

    • Proposed as answer by Xpert360 Wednesday, March 18, 2015 8:48 AM
    Tuesday, March 17, 2015 4:00 PM