【发布时间】:2019-03-06 00:50:19
【问题描述】:
如何将服务传递给承诺?我正在尝试创建一个承诺,该承诺将在所有 http 请求返回时解决。现在 this.jiraService 是未定义的。有没有办法把它传递给promise的构造函数?
export class JiraComponent {
private stories:Map<string,number> = new Map<string,number>();
constructor(private jiraService:JiraService) {
}
ngOnInit() {
this.jiraService.search()
.then(x => this.getKeys(x['issues']))
.then(keys => this.getLogs(keys))
.then(x => console.log(this.stories));
}
getKeys(issues:Array<Object>) {
let keys = new Array<string>();
for (var i of issues) {
keys.push(i['key']);
}
return keys;
}
getLogs(keys:Array<string>) {
return new Promise(function (resolve, reject) {
let count = 0;
for (var k of keys) {
this.jiraService.worklog(keys[0]).then(x => {
count++;
console.log(x);
if (!this.stories.get(k)) {
this.stories.set(k, x['timeSpentSeconds']);
}
else {
this.stories.set(k, this.stories.get(k) + x['timeSpentSeconds']);
}
if (count == keys.length) {
resolve();
}
});
}
});
}
【问题讨论】:
标签: angular typescript promise