Search code examples
javascriptjqueryes6-promisetimeline

vis timeline - items.add breaks loop when called from async function


I am adding multiple items to a vis timeline: The first adding is called inside a callback and works fine. The second time I am adding data which comes from an async ajax request and returns a promise.
getRocketChat().then(print);
The problem is now, that the loop is only executed once when I do the item adding. When I uncomment the item adding it works fine. I also tried around with timeline.redraw(), items.update() etc, but with no success. I absolutely have no idea why this does not work.

function print(channelAndMessages) {

    for (let i = 0; i < channelAndMessages.length; i++) {
        console.log(channelAndMessages[i]);

        for (let j = 0; j < channelAndMessages[i].messages.length; j++) {
            console.log("ChannelName " + getChannelName(channelAndMessages[i].messages[j].rid));

            let singleMsg = channelAndMessages[i].messages[j];

            items.add({
                id: "Chat" + singleMsg.id,
                group: 'einsatzltr',
                content: singleMsg.msg,
                start: moment(singleMsg.ts)

            });
            console.log(singleMsg.msg); //excecuted only once
        }
    }
}

Solution

  • I found the problem. It has nothing to do with the loop, but just with the id. I changed the item id to another function and now it works:

     id: "Chat" + i +" " + j,