Hi I am new to angular and javascript promise... I am trying to execute the following code and I have a few questions...
code here. and I can see a table filled with mock data
response;
promise_response;
data;
ngOnInit() {
this.response = this.bitbucket.commits.list({
include: 'master',
repo_slug: 'test',
workspace: 'test',
});
this.promise_response = Promise.resolve(this.response);
this.promise_response.then((data) => {
this.data = data.data.values;
console.log(data.data.values);
console.log('data');
});
console.log('test');
// replace below with actual data;
for (let i = 0; i < 5; i++) {
const item = {
id: '1',
comment: faker.random.words(),
time: faker.date.future(),
};
this.dataSource.push(item);
}
}
}
I am doing this "this.promise_response = Promise.resolve(this.response);" because I notice that when I console.log(this.response), I got "zoneAwarePromise"...which leads to google about promise and made me believe I need to somehow resolve it...
I added a few code to resolve the promise (not sure if it is correct), and I noticed that "test" is printed first..then "data".. and I wonder why ...
I moved the following inside this.promise_response.then but then my table is empty ... why...
// replace below with actual data;
for (let i = 0; i < 5; i++) {
const item = {
id: '1',
comment: faker.random.words(),
time: faker.date.future(),
};
this.dataSource.push(item);
}
Thanks!
Update:
Now the following works as expected. Thank you.
ngOnInit() {
this.bitbucket.commits
.list({
include: 'test',
repo_slug: 'test',
workspace: 'test',
})
.then((response) => {
this.data = response.data.values;
this.dataSource = this.data;
});
}