Skip to content

Instantly share code, notes, and snippets.

@xcaptain
Created November 6, 2017 16:39
Show Gist options
  • Save xcaptain/11d3df3437d5e3864112b620153ddf9b to your computer and use it in GitHub Desktop.
Save xcaptain/11d3df3437d5e3864112b620153ddf9b to your computer and use it in GitHub Desktop.
使用typescript + axios异步发送请求
import axios from 'axios';
async function getBaidu() {
return axios.get('http://www.baidu.com');
}
async function getBing() {
return axios.get('http://www.guazi.com');
}
axios.all([getBaidu(), getBing()])
.then(axios.spread(function (bdResp, biResp) {
let bdStatus = bdResp.status;
let biStatus = biResp.status;
console.log('baidu_status code is ', bdStatus, bdResp.data);
console.log('bing status code is ', biStatus, biResp.data);
}));
// tsconfig配置:
// target: ["es2017"]
// types: ["node"]
// 编译命令: tsc typescript_axios_async_request.ts
// TODO: 考虑结合RxJS来做事件监听
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment