0

I've been looking at the Netflix Falcor demo project, and have modified an example which adds a title, then immediately removes a title. When I run the following code repeatedly though, the last title in the list after the remove, (printed at 'titles after remove') is different every time.

Sometimes the last title is undefined, sometimes it still contains the pushed title, and sometimes the new title is appended causing the list of titles to slowly grow over time. This seems very buggy (or am I doing something wrong?)

model.get('genrelist[0].titles.length')
.then(function(z) {
    var length = z.json.genrelist[0].titles.length;
    model.getValue('genrelist[0].titles[0..' + length + '].name')
    .then(function(titles){
        console.log('titles before push:', titles);

        model.call('genrelist[0].titles.push',
            [{$type: "ref", value: ['titlesById', 3]}],
            ["name", "rating"], ["length"]
        ).then(function(x) {

            model.getValue('genrelist[0].titles[0..' + length + '].name')
            .then(function(titles){
                console.log('titles after push:', titles);
                var index = x.json.genrelist[0].titles.length - 1;
                model.call('genrelist[0].titles.remove', [index])
                .then(function(y) {

                    model.get('genrelist[0].titles[' + (index + 1) + ']["name", "year"]')
                    .then(function() {
                        model.get('genrelist[0].titles.length')
                        .then(function(w) {

                            model.getValue('genrelist[0].titles[0..' + length + '].name')
                            .then(function(titles){
                                console.log('titles after remove:', titles);
                            },jerror);
                        });
                    }, jerror);

                }, jerror);
            },jerror);
        }, jerror);
    },jerror);
}, jerror)
user1491819
  • 1,790
  • 15
  • 20
  • that's some nesting hell you've written there!! taking into consideration if how `x`, `length` and `index` are used, you possibly could minimise the nesting like so https://jsfiddle.net/r7a5gfvf/ (not an answer, but it may be a start) – Jaromanda X Jan 11 '17 at 23:42
  • This is absolutely horrible promise code. `return` a result (or promise) from every callback, [flatten the chain](http://stackoverflow.com/a/22000931/1048572) (for variables like `length` and `index` see [here](http://stackoverflow.com/q/28250680/1048572)), and put the error handler in a single `.catch(jerror)` in the end. – Bergi Jan 12 '17 at 01:12

0 Answers0