bbu*_*urc 1 javascript typescript ecmascript-6 es6-promise fetch-api
我需要链接promises来发出几个GET请求,并在将数据用于其他地方之前将其合并.我很难解决这两个承诺.我尝试在尝试使用之前返回两个promise的数组,.json()但这也不起作用.
activate() {
// data is from http://jsonplaceholder.typicode.com/photos and
// since there is not a photos2/ endpoint this is conceptual and
// represents batch importng
return Promise.all([
this.http.fetch('photos'),
this.http.fetch('photos2')
]).then(responses => {
console.log(responses); // see block of code below this for output
// how can I combine the two promises here so I can resolve them with 'then' below?
return responses[0].json(); // can return one response
// return responses; // doesn't work
}).then(data => {
console.log(data);
this.photos = data;
}).catch(err => {
console.log(err);
});
}
Run Code Online (Sandbox Code Playgroud)
console.log的输出(响应); :
[Response, Response]
0: Response
body: (...) // readablebytestream
bodyUsed : false
headers : Headers
ok : true
status : 200
statusText : "OK"
type : "cors"
url : "http://jsonplaceholder.typicode.com/photos"
__proto__ : Object
1 : Response
....etc
Run Code Online (Sandbox Code Playgroud)
谢谢!
你似乎在寻找
return Promise.all([responses[0].json(), responses[1].json()]);
Run Code Online (Sandbox Code Playgroud)
或者只是做
this.photos = Promise.all([
this.http.fetch('photos').then(response => response.json()),
this.http.fetch('photos2').then(response => response.json())
])
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
520 次 |
| 最近记录: |