我有一个要遵循的 url 列表,并希望将结果存储在一个数组中。当前的实现是使用 map 来获取和 when.all 等待。如下所示:
const when = require('when');
}).then((responseJson) => {
return responseJson._embedded.leases.map(
lease => fetch(lease._links.self.href, {headers: {'Access-Control-Expose-Headers': 'ETag'}}));
}).then(leasePromises => {
return when.all(leasePromises);
}).then((leases) => {
var obj = leases.toString();
console.log("leasesJSON = " + obj);
console.log("leases[0]0= " + leases[0]);
console.log("leases[0]1= " + JSON.stringify(leases[0]));
console.log("leases[0]2= " + leases[0].json());
console.log("leases[0]3= " + JSON.stringify(leases[0].json()));
this.setState({
isLoading: false,
leases: leases,
attributes: Object.keys(this.schema.properties)
});
}).catch((error) => {
console.error("loadDataFromServer error = " + error);
});
但是 render() 方法抱怨“租用”对象是空的。
追踪结果:
leasesJSON = [object Response],[object Response]
leases[0]0= [object Response]
leases[0]1= {}
leases[0]2= [object Promise]
leases[0]3= {}
所以我的问题是:
- 如何打印出promise和response信息?
- 我获取对象列表的方式是否正确?
- 如果没有,那么如何纠正它们?