How to wait a request response and return the value?

笑着哭i 提交于 2019-12-04 20:34:27

You can use callbacks in the following way.

function getRequest(requestObject, callback) {
    // some code
    requestToServer({
       url : urlInformation,
       method : 'GET',
       headers : jsonObject
    }, function(error, response ,body) {
       // todo response controlling
       var responseObject = response.headers;
       responseObject.body = body;
       callback(responseObject);
    }); 
}

And

// Controlling the submitted request
exports.requestController = function(requestObject) {
   var method = requestObject['methodInformation'];

   // Selecting the method
   if(method == "GET")
      getRequest(requestObject, function(resultObject){
          console.log(JSON.stringify(resultObject));
      });

   //some code
}

Hope, it helps.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!