Javascript 如何使用异步任务执行多个请求,例如何时完成所有请求
您好,我亲爱的程序员伙伴,我想做一个多http请求使用承诺知道何时http请求完成,我有这个代码,但不工作,我到这里来为您的帮助 这是我的代码:Javascript 如何使用异步任务执行多个请求,例如何时完成所有请求,javascript,angularjs,promise,ionic2,httprequest,Javascript,Angularjs,Promise,Ionic2,Httprequest,您好,我亲爱的程序员伙伴,我想做一个多http请求使用承诺知道何时http请求完成,我有这个代码,但不工作,我到这里来为您的帮助 这是我的代码: promisesMappingIp(){ var promises = []; for(var i = 1; i < 255; i++){ promises.push(this.http.get(`http://192.168.1.`+i+`/hello`).map((res:Response) => {
promisesMappingIp(){
var promises = [];
for(var i = 1; i < 255; i++){
promises.push(this.http.get(`http://192.168.1.`+i+`/hello`).map((res:Response) => {
try{
return res.json();
}catch(err){
return {};
}
}));
}
Promise.all(promises).then(function(){
promises.forEach((x) =>{
x.subscribe(data => {
});
});
}).catch((err) =>{
console.error(err);
});
}
promisesMappingIp(){
var承诺=[];
对于(变量i=1;i<255;i++){
promises.push(this.http.get(`http://192.168.1.`+i+`/hello`)。map((res:Response)=>{
试一试{
返回res.json();
}捕捉(错误){
返回{};
}
}));
}
Promise.all(promises).then(function(){
承诺。forEach((x)=>{
x、 订阅(数据=>{
});
});
}).catch((错误)=>{
控制台错误(err);
});
}
在我的控制台里,我犯了一个错误:
错误对象{u body:ERROR,status:0,ok:false,statusText:,headers:Object,type:3,url:null}
我正在为一个学校项目使用Ionic 2,我真的需要你的帮助,我想映射所有返回我一个响应的ip=“问候!”
多谢各位 您可以使用
Observable
import {Observable} from 'rxjs/Rx';
promisesMappingIp() {
var promises = [];
for (var i = 1; i < 255; i++) {
promises.push(this.http.get(`http://192.168.1.` + i + `/hello`));
}
Observable.forkJoin(promises)
.subscribe((response) => {
console.log(response[0]);
console.log(response[1]);
});
}
从'rxjs/Rx'导入{Observable};
承诺书{
var承诺=[];
对于(变量i=1;i<255;i++){
promises.push(this.http.get(`http://192.168.1.`+i+`/你好`);
}
可观察。forkJoin(承诺)
.订阅((响应)=>{
console.log(响应[0]);
console.log(响应[1]);
});
}
发生快速故障。这意味着一旦第一个承诺失败,它就会失败
您需要拦截故障,而不是传播它,如下所示:
promises.push(this.http.get(`http://192.168.1.`+i+`/hello`).map((res:Response) => {
try{
return res.json();
}catch(err){
return {};
}
}).catch(err=> ({ error: err })));
我已经使用XMLhttprequest解决了我的问题,这是我的代码,我希望这能帮助任何有同样问题的人:
getService(ip: string, param: string): Promise<any> {
let url: string = `http://${ip}/${param}`;
return new Promise((r,j) =>{
var xhr = new XMLHttpRequest();
xhr.open("GET", url, true);
xhr.onload = function(e){
if (xhr.readyState === 4) {
r(ip);
}
}
xhr.onerror = function(err){
r(null);
}
xhr.send(null);
});
}
getService(ip:string,param:string):承诺{
让url:string=`http://${ip}/${param}`;
返回新承诺((r,j)=>{
var xhr=new XMLHttpRequest();
xhr.open(“GET”,url,true);
xhr.onload=函数(e){
if(xhr.readyState==4){
r(ip);
}
}
xhr.onerror=函数(err){
r(空);
}
xhr.send(空);
});
}
这是我用来打电话的方法:
getIpList(){
let promises = [];
for(var i = 1; i < 255; i++){
promises.push(this.nodeProvider.getService(`192.168.1.${i}`, 'hello'));
}
Promise.all(promises).then((arrayIp) => {
let clean = arrayIp.filter(ip => !!ip);
});
}
getIpList(){
让承诺=[];
对于(变量i=1;i<255;i++){
promises.push(this.nodeProvider.getService(`192.168.1.${i}`,`hello');
}
承诺。所有(承诺)。然后((arrayIp)=>{
让clean=arrayIp.filter(ip=>!!ip);
});
}
我希望我能解决你的问题