js 异步同步链

const chain = (runnable) => {
    let filterChain = {
    };
    let currentFilter = filterChain;
    let config = {
        doFilter: (filter) => {
            currentFilter["filter"] = filter;
            currentFilter["child"] = {};
            currentFilter = currentFilter["child"];
            return config;
        },
        doChain: (filterChain, result) => {
            let currentHandler = filterChain.filter;
            if (filterChain.child != "none") {
                let child = filterChain.child;
                currentHandler(result, (result) => {
                    config.doChain(child, result);
                });
            } else {
                currentHandler(result);
            }
        },
        doFinish: (filter) => {
            currentFilter["filter"] = filter;
            currentFilter["child"] = "none";
            let success = (res) => {
                config.doChain(filterChain, res); 
            };
            runnable(success);
        }
    };

    return config;
}
module.exports = chain;

保证异步请求按顺序执行

 chain((success) => {

      wx.getSetting({
        success: res => {
          if (res.authSetting['scope.userInfo']) {
            success();
          }
        }
      })
    }).doFilter((result, success) => {
      // 获取用户信息
      wx.getUserInfo({
        success: res => {
          this.globalData.userInfo = res.userInfo;
          if (this.userInfoReadyCallback) {
            this.userInfoReadyCallback(res)
          }
          success({
            gender: res.userInfo.gender
          });
        }
      })
    }).doFilter((result, success) => {
      // 登录
      wx.login({
        success: res => {
          result["code"] = res.code;
          success(result);
        }
      })
    }).doFilter((result, success) => {
      // 获取位置信息
      wx.getLocation({
        type: 'wgs84',
        success: res => {
          result["latitude"] = res.latitude;
          result["longitude"] = res.longitude;
          success(result)
        }
      })
    }).doFinish((result) => {
      // 获取token
      let that =this;
      api({
        url: "/wechat/openid",
        data: result,
        method: "POST"

      }).before((header, data) => {
      
        return true;
      }).then((res) => {
        console.log(res);
        if (res.statusCode == 200) {
          let data = res.data.data;
          let token = data.token; 
          let user = data.user;
          that.globalData.userInfo["token"] = token;
          that.globalData.userInfo["region"] = [user["province"] == null ? "" : user["province"], user["city"] == null ? "" : user["city"], user["county"] == null ? "" : user["county"]];
          for (let key in user) {
            that.globalData.userInfo[key] = user[key];
          }

        }
      }).fail(() => {
        console.log("fail 123");
        let testData = { "code": 200, "data": { "user": { "id": 1, "name": "冯林", "concatWay": "17751236199", "birthday": "1984-08", "address": "松岭镇美岸青城61#1701", "gender": 1, "no": "8f9efdb57920f18d965b342f1960d92c", "province": "江苏省", "city": "苏州市", "county": "吴江区" }, "token": { "access_token": "eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJubyI6IjhmOWVmZGI1NzkyMGYxOGQ5NjViMzQyZjE5NjBkOTJjIiwidXNlcl9uYW1lIjoie25vPThmOWVmZGI1NzkyMGYxOGQ5NjViMzQyZjE5NjBkOTJjLCBvcGVuSWQ9b3EzOGc1V0I0REd1Z1dGRkdNWW0tQmlLZWhRcywgdXNlcklkPTF9Iiwib3BlbklkIjoib3EzOGc1V0I0REd1Z1dGRkdNWW0tQmlLZWhRcyIsInNjb3BlIjpbImFsbCJdLCJleHAiOjE1NTYyOTQ1NzIsInVzZXJJZCI6MSwianRpIjoiMzc3ODQ0ZTItOTA3NC00NjY3LWI1OWEtMzI0M2VhZjQyZjVlIiwiY2xpZW50X2lkIjoid3g2N2I5MzRhMmUxMzJmZTA1In0.YVNtrq0dZ803DFXAMLZoDOXA0M1rkHFDyak8ZicX-N6NYWpRXREediEchMU44E7A2qhGUa5pYno-2g7Fr9pkNHWxeWtbumN0U9ehLGlAIVyQdGdS6-4sXIokNUsm9ZpBeP2wPOoXf_BqEehESyi4-D2uQZ-toPU8yUPL7SCCkdJbXHn5GFgDDJ803uoKCdO--GK3RRUl5BZVViI2z-SwnoRJUrBmxnVtFE-QTo_Vd6UP6BEY0SSye3PacR0RwbnOOMXPmQDKcJrzmM02CZxoPwK58SVPFLKfjCtQ2lM92-ZJ_oEPDv3czsp-ddyBm7twe56EVHgd3eotdcEb_2-pjg", "token_type": "bearer", "refresh_token": "eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJubyI6IjhmOWVmZGI1NzkyMGYxOGQ5NjViMzQyZjE5NjBkOTJjIiwidXNlcl9uYW1lIjoie25vPThmOWVmZGI1NzkyMGYxOGQ5NjViMzQyZjE5NjBkOTJjLCBvcGVuSWQ9b3EzOGc1V0I0REd1Z1dGRkdNWW0tQmlLZWhRcywgdXNlcklkPTF9Iiwib3BlbklkIjoib3EzOGc1V0I0REd1Z1dGRkdNWW0tQmlLZWhRcyIsInNjb3BlIjpbImFsbCJdLCJhdGkiOiIzNzc4NDRlMi05MDc0LTQ2NjctYjU5YS0zMjQzZWFmNDJmNWUiLCJleHAiOjE1NTg4NDMzNzIsInVzZXJJZCI6MSwianRpIjoiY2ZkNGYyNTYtOWFmNi00YmNlLTk4YzktN2M2OTFhNTlmYTg3IiwiY2xpZW50X2lkIjoid3g2N2I5MzRhMmUxMzJmZTA1In0.QzlelpMLFPRzx9wPirKH5USSbzZNKF6E8oag9eaqwys36kRM6Q-ShpCjqbawQWg2hYbj5Rx5LqeDa7GfjlkHwd_GeFI3ffaxKaJ8W-sVk8ycxNJQ7MK8dpCrYCEfVI34PIBioGGOkLtfBM1TckIjcv1hA9hZETQGQaFAMm4mcQPaZc4P54U7o-O0L2DqeWriFcXgbe3drXy87IKsMQyX2v6rABa0C3P7oeUo4-Skd9wuHArECpQsnx8M_ls5e4QlSewvo-5TDIkBg70v-zRTMeCLInSvJ7glo0JNLjNLFTS8e-WvZvAS_I9Y8rYB3Bg5EL4WxoZKRhT2s9JHx3DXaQ", "expires_in": 43199, "scope": "all", "no": "8f9efdb57920f18d965b342f1960d92c", "openId": "oq38g5WB4DGugWFFGMYm-BiKehQs", "userId": 1, "jti": "377844e2-9074-4667-b59a-3243eaf42f5e" } }, "message": null, "success": true };
        let token = testData.data.token;
        let user = testData.data.user;
        that.globalData.userInfo["token"] = token;
        that.globalData.userInfo["region"] = [user["province"] == null ? "" : user["province"], user["city"] == null ? "" : user["city"], user["county"] == null ? "" : user["county"]];
        for (let key in user) {
          that.globalData.userInfo[key] = user[key];
        }
      });

你可能感兴趣的:(js 异步同步链)