Javascript 所有顺序
我正在尝试执行3个带有承诺的web服务,我需要在所有这些服务都执行后,如果可能的话,按顺序返回3个服务的信息。 我有这个 这是我的服务Javascript 所有顺序,javascript,angular,promise,angular2-services,Javascript,Angular,Promise,Angular2 Services,我正在尝试执行3个带有承诺的web服务,我需要在所有这些服务都执行后,如果可能的话,按顺序返回3个服务的信息。 我有这个 这是我的服务 getServices(url: string): Promise < any > { return this.http.get(CoordinadoresService.BASEURL + url) .toPromise() .then(response => { return response.json();
getServices(url: string): Promise < any > {
return this.http.get(CoordinadoresService.BASEURL + url)
.toPromise()
.then(response => {
return response.json();
})
.catch(err => err);
}
谢谢。您的实现存在一些问题
HttpClient
,则不必map
,然后在响应中调用json
this.services.getServices(url)
的然后返回。因此,Promise.all中没有响应
从'@angular/common/http'导入{HttpClient};
...
构造函数(私有http:HttpClient){}
....
getServices(url:string):承诺{
返回此.http.get(CoordinadoresService.BASEURL+url)
.toPromise();
}
getOffices(){
这个.oficinas[“coordinadores”]=[];
让data=this.util.getLocalStorage(“coordinadores”);
让承诺=[];
如果(数据){
for(设i=0;i{
log('两个承诺都已解决',数据);
});
}
}
私有getDataFromAPI(url){
返回此.services.getServices(url)
.那么(
响应=>{
response[“Coordinator”]=response.Coordinator;
这个.oficinas[“coordinadores”].push(响应)
返回响应;
},
err=>err
);
}
console.log中出现未定义的原因是此处的代码
promises.push(this.services.getServices(url)
.then(response => {
response["coordinador"] = response.coordinador;
this.oficinas["coordinadores"].push(response);
// nothing returned
}, err => err));
由于没有返回任何内容(如有说明),因此推入承诺的承诺将解析为未定义的
只需添加一个返回响应
注:response[“coordinador”]=response.coordinador
是多余的,就像说a=a
——所以我不会在下面的代码中重复它
另外,this.oficinas[“coordinadores”]
是this.oficinas.coordinadores
-因此将使用后者
promises.push(this.services.getServices(url)
.then(response => {
this.oficinas.coordinadores.push(response);
// something returned
return response;
}, err => err));
至于你问题的另一部分。。。你想“按顺序”这样做
如果您可以使用async
/wait
——这将使更改变得非常简单
async getOffices() { // add async keyword
this.oficinas.coordinadores. = [];
let data = this.util.getLocalStorage("coordinadores");
let results = []; // no longer dealing directly with promises, so lets rename this
if (data != undefined) {
for (let i = 0; i < Object.keys(data.coordinadores).length; i++) {
let url = `getOficinas/${data.coordinadores[Object.keys(data.coordinadores)[i]].ip}/${Object.keys(data.coordinadores)[i]}`;
// await the promise
let result = await this.services.getServices(url).then(response => {
this.oficinas.coordinadores.push(response);
return response;
}, err => err);
// push the result
results.push(result);
}
// output the result
console.log('Both promises have resolved', results);
}
}
只是看起来一团糟,让我建议一个替代方案
async getOffices() {
this.oficinas.coordinadores. = [];
let data = this.util.getLocalStorage("coordinadores");
let results = [];
if (data != undefined) {
for (let [key, {ip}] of Object.entries(data.coordinadores)) {
const url = `getOficinas/${ip}/${key}`;
let result = await this.services.getServices(url).then(response => {
this.oficinas.coordinadores.push(response);
return response;
}, err => err);
results.push(result);
}
console.log('Both promises have resolved', results);
}
}
使用
HttpClient
或Http
?通过调用然后调用打开Promise
。因此,您的Promises数组实际上不会有任何类似的承诺。解决办法是什么?你说的“按顺序”是指这三个承诺需要一个接一个地完成吗?很高兴这有帮助。:)
promises.push(this.services.getServices(url)
.then(response => {
this.oficinas.coordinadores.push(response);
// something returned
return response;
}, err => err));
async getOffices() { // add async keyword
this.oficinas.coordinadores. = [];
let data = this.util.getLocalStorage("coordinadores");
let results = []; // no longer dealing directly with promises, so lets rename this
if (data != undefined) {
for (let i = 0; i < Object.keys(data.coordinadores).length; i++) {
let url = `getOficinas/${data.coordinadores[Object.keys(data.coordinadores)[i]].ip}/${Object.keys(data.coordinadores)[i]}`;
// await the promise
let result = await this.services.getServices(url).then(response => {
this.oficinas.coordinadores.push(response);
return response;
}, err => err);
// push the result
results.push(result);
}
// output the result
console.log('Both promises have resolved', results);
}
}
let url = `getOficinas/${data.coordinadores[Object.keys(data.coordinadores)[i]].ip}/${Object.keys(data.coordinadores)[i]}`;
async getOffices() {
this.oficinas.coordinadores. = [];
let data = this.util.getLocalStorage("coordinadores");
let results = [];
if (data != undefined) {
for (let [key, {ip}] of Object.entries(data.coordinadores)) {
const url = `getOficinas/${ip}/${key}`;
let result = await this.services.getServices(url).then(response => {
this.oficinas.coordinadores.push(response);
return response;
}, err => err);
results.push(result);
}
console.log('Both promises have resolved', results);
}
}