收藏
回答

wx.requset顺序问题




为什么是4先被输出呢?

最后一次编辑于  2017-11-21
回答关注问题邀请回答
收藏

6 个回答

  •         
            
    2017-11-21

    Randy 

    这个是异步请求,Success作为回调函数是晚于同步代码(4)的。

    如果对异步不了解,可以用Promise套一下。

    test: function () {

        let param = {

          Channel: "H5",

          ProductId: this.productId,

          Version: 70100

        };

        console.log(2);

        let url = "xxxxx";

        return new Promise((resolve, reject) => {

          cwx.request({

            url: url,

            data: param,

            header: {

              'content-type': 'application/json'

            },

            method: 'POST',

            success: function (res) {

              console.log(3);

              resolve(res);

            },

            fail: function (ex) { 

              reject(ex);

            }

          })

        });

      },


      onShow: function(){

        console.log(1);

        this.test()

        .then((res)=>{

          console.log(4);

        })

        .catch((ex)=>{

          //异常处理

        })

      },


    17:30

    谢啦!受教了

    2017-11-21
    有用
    回复
  • Randy 🦈
    Randy 🦈
    2017-11-21

    这个是异步请求,Success作为回调函数是晚于同步代码(4)的。

    如果对异步不了解,可以用Promise套一下。

    test: function () {

        let param = {

          Channel: "H5",

          ProductId: this.productId,

          Version: 70100

        };

        console.log(2);

        let url = "xxxxx";

        return new Promise((resolve, reject) => {

          cwx.request({

            url: url,

            data: param,

            header: {

              'content-type': 'application/json'

            },

            method: 'POST',

            success: function (res) {

              console.log(3);

              resolve(res);

            },

            fail: function (ex) { 

              reject(ex);

            }

          })

        });

      },


      onShow: function(){

        console.log(1);

        this.test()

        .then((res)=>{

          console.log(4);

        })

        .catch((ex)=>{

          //异常处理

        })

      },


    2017-11-21
    有用
    回复
  •         
            
    2017-11-21

    Randy 

    异步不理解的话,试试Promise

    17:18

    Promise还没用过这个

    2017-11-21
    有用
    回复
  •         
            
    2017-11-21


    是我理解错了么


    2017-11-21
    有用
    回复
  • Randy 🦈
    Randy 🦈
    2017-11-21

    异步不理解的话,试试Promise

    2017-11-21
    有用
    回复
  • 管凯旋
    管凯旋
    2017-11-21

    异步啊兄弟

    2017-11-21
    有用
    回复
登录 后发表内容