简体   繁体   English

使用 JavaScript Trello API 链接 promise

[英]Chaining promises with JavaScript Trello API

I'm trying to use the JavaScript Trello API to compile a list of cards that can be from any of the users boards that fall under any list with the name "todo".我正在尝试使用 JavaScript Trello API 来编译一个卡片列表,这些卡片可以来自属于任何名称为“todo”的任何列表的任何用户板。

I successfully was able to get an array of list ID's from all boards with the name "todo".我成功地从名称为“todo”的所有板中获取了一系列列表 ID。

...

}).then(function(list_data) {

        console.log("List Data", list_data);
        console.log("List Data Length", list_data.length);

        var cards_arr = [];

        $.each(list_data, function(index, value) {

            Trello.get("/lists/" + value.id + "/cards?fields=name,shortUrl")
            .then(function(cards) {

                $.each(cards, function(index, card) {
                    cards_arr.push(card.id);
                })

            })

        })

        console.log("Cards Array", cards_arr);

        return cards_arr;

    })

When I look at the console log where it's showing my "list_data" variable I get an array of 6 items, but for some reason the length of the array is equal to 0 and doesn't allow me to loop through the "empty" array.当我查看显示我的“list_data”变量的控制台日志时,我得到一个包含 6 个项目的数组,但由于某种原因,数组的长度等于 0 并且不允许我遍历“空”数组.

Console Log SnapShot控制台日志快照

I feel that I may not be using the Trello API in the most effective way, so I will go ahead and post the full code below.我觉得我可能没有以最有效的方式使用 Trello API,所以我会继续在下面发布完整的代码。

Any help would be greatly appreciated.任何帮助将不胜感激。 Thanks谢谢

    var boards_arr = [];

    // ==== LOOP THROUGH BOARDS
    Trello.get("/member/me/boards?fields=name,id,idOrganization")
    .then(function(boards) {

        $.each(boards, function(index, board) {
            if (board.idOrganization != "5112df8dad3201247401349e") return;
            boards_arr.push(board.id);
        });

        return boards_arr;

    }).then(function(board_data) {

        var lists_arr = [];

        console.log("Board Data", board_data);

        $.each(board_data, function(index, value) {

            Trello.get("/boards/" + value + "/lists?fields=id,name")
            .then(function(lists) {

                $.each(lists, function(index, list) {

                    var listName = list.name.replace(" ", "").toLowerCase();
                    if (listName != "todo") return;

                    lists_arr.push(list.id);
                });

            });

        });

        return lists_arr;

    }).then(function(list_data) {

        console.log("List Data", list_data);
        console.log("List Data Length", list_data.length);

        var cards_arr = [];

        $.each(list_data, function(index, value) {

            Trello.get("/lists/" + value.id + "/cards?fields=name,shortUrl")
            .then(function(cards) {

                $.each(cards, function(index, card) {
                    cards_arr.push(card.id);
                })

            })

        })

        console.log("Cards Array", cards_arr);

        return cards_arr;

    })

Since you are running an asynchronous operation ( Trello.get ) inside your $.each callback, the elements won't be added to the array until after these have completed, which is after $.each returns.由于您在$.each回调中运行异步操作 ( Trello.get ),因此在这些完成之后(即$.each返回之后)元素不会添加到数组中。 So no elements have been added by the time you call console.log(cards_arr) .因此,在您调用console.log(cards_arr)时尚未添加任何元素。

What you should do instead, is to store the promises you get from Trello.get in an array, then call Promise.all to wait for all to complete, and then you build the cards_arr array when you have all the results.您应该做的是,将您从Trello.get获得的承诺存储在一个数组中,然后调用Promise.all等待所有完成,然后在您获得所有结果时构建cards_arr数组。 Since this separates the asynchronous and synchronous operations, you can be sure that cards_arr is actually complete when you call console.log afterwards.由于这将异步和同步操作分开,因此您可以在之后调用console.log时确定 card_arr 实际上是完整的。

promise
  .then(function(list_data) {
    console.log("List Data", list_data);
    console.log("List Data Length", list_data.length);

    // do all operations and store results in an array
    var resultPromises = list_data.map(function(value) {
      return Trello.get("/lists/" + value.id + "/cards?fields=name,shortUrl")
    });

    // wait for all operations to complete
    return Promise.all(resultPromises);
  })
  .then(function(results) {
    // create cards_arr from results
    var cards_arr = [];
    $.each(results, function(index, cards) {
      $.each(cards, function(index, card) {
        cards_arr.push(card.id);
      });
    });

    // this should print the correct array
    console.log("Cards Array", cards_arr);

    return cards_arr;
  })

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM