Is there a way to pass a service into a promise? I am currently working on a promise that will only resolve once all the http requests are complete. However, I am facing an issue where this.jiraService is undefined. Is there a method to pass it to the constructor of the 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();
}
});
}
});
}