Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/sharepoint/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript while循环中的嵌套承诺不同步工作_Javascript_Sharepoint_Asynchronous_Promise_Project Server - Fatal编程技术网

Javascript while循环中的嵌套承诺不同步工作

Javascript while循环中的嵌套承诺不同步工作,javascript,sharepoint,asynchronous,promise,project-server,Javascript,Sharepoint,Asynchronous,Promise,Project Server,我正在用javascript编写一个SharePoint应用程序(用于Project Server),并使用异步调用从服务器查询数据 这些异步调用相互依赖,甚至是循环调用 要了解问题,请执行以下操作: Project Server上有多个项目。我想阅读每一个项目。然后我一个接一个地浏览这些项目,并阅读它们的属性。它是这样的: $(document).ready(function () { projContext = PS.ProjectContext.get_current();

我正在用javascript编写一个SharePoint应用程序(用于Project Server),并使用异步调用从服务器查询数据

这些异步调用相互依赖,甚至是循环调用

要了解问题,请执行以下操作:

Project Server上有多个项目。我想阅读每一个项目。然后我一个接一个地浏览这些项目,并阅读它们的属性。它是这样的:

$(document).ready(function () {
    projContext = PS.ProjectContext.get_current();

    //I read all the projects when the page loads
    var projectspromise = GetAllProjects();
    projectspromise.done(function (resultprojects) {
        //if I get the data i use the result in the next function
        GoThroughProjects(resultprojects);
    });

    projectspromise.fail(function (result) {
        var error = result;
    console.log(error);
    });

});

function GoThroughProjects(projectlist) {
    var projenum = projectlist.getEnumerator();
    while (projenum.moveNext()) { //i'm going through the projects one by one
        var project = projenum.get_current();
        alert("Y - " + project.get_name()); //Y ALERT

        GetProperties(project).then(
            function (resultproperties) {
                alert("X - Got the properties"); //X ALERT
            },
            function (sender, args) {
                alert(args.get_message());
            }
       );
   }
}
在本例中,我将按顺序获取警报,这对我来说并不太合适。 以Y开头的警报都会一个接一个地显示,中间没有任何X警报。(例如,如果有3个项目,它们会像YYYXXX,而不是yx)

我如何链接它们,使它们以更同步的方式运行

所以在某种伪代码中,它会是这样的:

First Async Call to read all projects
for each project
{
    Second Async Call to read the selected projects data
    for each property
    {
        do something with it / maybe third level of async call
    }
}

原因是
GetProperties
是一个异步调用。循环在发出调用后继续,在处理第一个返回的GetProperties调用之前打印出下一个Y。如果你想让它更同步,你通常可以选择暂停执行直到调用返回(比如C#中的“wait”)或者发出同步调用——虽然我不知道你正在使用的API,但是你需要浏览API文档,找到适合你情况的那些方法的名称。没有其他选择。这里只允许异步调用。但应该有一种方法将这些调用正确地链接在一起。因为我不想在不使用前一个属性的情况下进入下一个项目。或者使用.then()链接while循环的每个迭代