weixin_33708432 2018-03-22 22:01 采纳率: 0%
浏览 21

许诺小姐了解

Hi i have simple code like that:

jQuery(function ($) {
    function Item(val) {
        this.val = val
        var self = this;
        this.generateTrDOM = function () {
            var tr = '<tr>';
            tr += '<td>' + self.val + '</td>';
            tr += '</tr>';
            return tr;
        };
    }

    function ItemsCollection() {
        this.someHandler = $('#some_table_id');
        this.data = [];
        var self = this;


        this.getData = function () {
            return new Promise(function (resolve, reject) {
                $.post(base_url + 'controller/action', {
                    action_type: 'get_items'
                }, function (data) {
                    self.prepareData(data).then(resolve());
                }, "json");
            });
        };
        this.prepareData = function (data) {
            return new Promise(function (resolve, reject) {
                for (var x = 0; x < data.length; x++) {
                    self.data.push(
                        new Item(data[x])
                    );
                }
            });
        };
        this.updateTable = function () {
            for (var x = 0; x < self.data.length; x++) {
                self.someHandler.append(self.data[x].generateTrDOM());
            }

        };
    }

    var data = new ItemsCollection();
    data.getData() //this is done second
        .then(
            data.updateTable(); //this is done first - i dont not want to
        );
});

I want to data.updateTable() took place after the data.getData() after AJAX request but its start before. I read about the promises and in my other project its worked very well.

Which I did not understand. Thanks for some advices.

  • 写回答

2条回答 默认 最新

  • weixin_33733810 2018-03-22 22:10
    关注

    try to avoid nested promises to keep your async code linear.

    check this post : Are nested promises normal in node.js? ( i know this is not node but it's the same idea :P )

    and to do so, you'll need to call getData, resolve it, then call prepareData and resolve it , then updateTable like :

    jQuery(function ($) {
        function Item(val) {
            this.val = val
            var self = this;
            this.generateTrDOM = function () {
                var tr = '<tr>';
                tr += '<td>' + self.val + '</td>';
                tr += '</tr>';
                return tr;
            };
        }
    
        function ItemsCollection() {
            this.someHandler = $('#some_table_id');
            this.data = [];
            var self = this;
    
    
            this.getData = function () {
                return new Promise(function (resolve, reject) {
                    $.post(base_url + 'controller/action', {
                        action_type: 'get_items'
                    }, function (data) {
                        resolve(data); 
                    }, "json");
                });
            };
    
            this.prepareData = function (data) {
                return new Promise(function (resolve, reject) {
                    for (var x = 0; x < data.length; x++) {
                        self.data.push(
                            new Item(data[x])
                        );
                    }               
                    resolve(self.data);
                });
            };
    
            this.updateTable = function (data) {
                for (var x = 0; x < self.data.length; x++) {
                    self.someHandler.append(data[x].generateTrDOM());
                }
            };
        }
    
        var data = new ItemsCollection();
    
        data.getData()
            .then((result) => {
                return data.prepareData(result);            
            })  
            .then((prearedData) => {
                return data.updateTable(prearedData); 
            });
    });
    

    here's a working fiddle for that https://jsfiddle.net/aejvsxke/5/ ( i replaced your $post with a $.get to a fake api and added console.log in each function for testing purposes, so you can see the order of the execution )

    use this to compare the two codes and see the differences : https://www.diffchecker.com/diff

    hint : for this.getData you can return the $.post directly as it returns a promise itself (no need to wrapp it in a promise and no need for the callback ) as of jQuery 1.5 : https://api.jquery.com/jquery.post/

    so it becomes like this :

    this.getData = function () {
        return $.post(base_url + 'controller/action', {
                    action_type: 'get_items'        
                }, "json"); 
    };
    

    i hope this resolves your problem ;) or at least gives you an idea :P

    评论

报告相同问题?

悬赏问题

  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测