LightSwitch的HTML如何知道什么时候异步迭代完成 [英] Lightswitch html how to know when async iteration is done

查看:159
本文介绍了LightSwitch的HTML如何知道什么时候异步迭代完成的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在另一个异步操作异步操作。我不知道我怎么能知道,当一切都已经完成。

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

下面是我的code:

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) {

首先,我加载 ConfirmedWaiters 集合。一旦完成我遍历每个实体并加载子实体异步的,所以我想知道迭代完成时!?但问题是,它返回的时候了,因为它的异步,所以我怎么能等到迭代完成,然后调用 operation.complete()

First I load the ConfirmedWaiters collection. Once it is completed 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 call operation.complete()?

推荐答案

为了应对这种类型的异步挑战,你需要加入你的承诺。

In order to tackle this type of asynchronous challenge, you need to join your promises.

下面的博客文章提供了有关承诺的一些背景,是适合任何LightSwitch HTML客户端开发一个有用的读取(它总是值得铭记的LightSwitch HTML很大程度上WinJS基于任何WinJS承诺相关的材料是值得一读): -

The following blog posts provide some background on promises and are 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): -

<一个href=\"https://blogs.msdn.microsoft.com/lightswitch/2014/02/04/promises-in-lightswitch-justin-anderson/\"相对=nofollow> LightSwitch中的承诺(贾斯汀·安德森)

<一个href=\"http://blogs.msdn.com/b/windowsappdev/archive/2013/06/11/all-about-promises-for-windows-store-apps-written-in-javascript.aspx\"相对=nofollow>关于承诺(用于使用JavaScript编写的Windows Store应用程序)

根据的加盟并行的承诺的方式覆盖在这些博客文章的第二,你的code应该结束了类似应达到预期的效果(虽然它可能会遇到性别平等例外以下; - )

Based on the 'Joining parallel promises' approach covered in the second of these blog posts, 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) {

这应该可以做的伎俩。如果没有,上传样本项目,以提供更多的背景可能的帮助。

Hopefully this should do the trick. If not, uploading a sample project to provide more background may help.

这篇关于LightSwitch的HTML如何知道什么时候异步迭代完成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆