封装一个axios方法。解决异步问题

function logina(id, password) {
let url = "url";
let json = {

loginid: id,
password: password

};
let result = Post(url, json, null);
console.log(1, result);
return result;
}

function Post(api, post, headers) {
let result = false;
axios.post(api, post, { headers: headers })

.then((function(response) {
  console.log("post", response);
  return response;
}))
.catch(function(err) {
  console.log(err);
  return result;
});

}

代码如上所示,如果成功的话 console执行的result为undefined。之后才会返回post相应数据,如何让result返回的是response的数据呢

阅读 3k
1 个回答
function Post(api, post, headers,callback) {
let result = false;
axios.post(api, post, { headers: headers })

.then((function(response) {
  console.log("post", response);
  callback(res);
  return response;
}))
.catch(function(err) {
  console.log(err);
  return result;
});
}

成功回调返回res
post(apiurl, post, header,function(res){

 console.log(res)

})

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进