Data not ready when needed

Multi tool use
Multi tool use









0















I need to load some data from the server before further processing. So have I have this function:



async load() 
await this.reloadClients()
this.findTimeSheets();


findTimeSheets()
for (const client of this.clients)
console.log('Client: ' + client.lastName);




This function gets called when clicking a button on the component.



I need the function this.timeSheets to wait until this.reloadClients is done processing, so the data is ready. This is the the reload function, it should load a list of clients and store it in this.clients:



reloadClients() 
this.clientService.search(
page: '0',
query: 'assigner.id:' + this.selectedAssigner.id,
size: '250',
sort: '')
.subscribe((res: HttpResponse<Client>) => this.clients = res.body; this.init(res.body); , (res: HttpErrorResponse) => this.onError(res.message));



What happens now is that I need to click the button twice to get the correct data. So the loop in findTimeSheets() works after the second click (printing data to console).



My approach was to use await, but somehow this doesn't work.



As I'm new to Angular, I need some help resolving this.










share|improve this question

















  • 1





    return this.clientServiceSearch({...

    – Wainage
    Nov 13 '18 at 20:55











  • Won't help, as i'm subscribing to the result returned by the service.

    – Fish-Guts
    Nov 13 '18 at 21:04






  • 1





    It will. Makes the async call "wait". Otherwise it returns "undefined" before clientService gets to complete

    – Wainage
    Nov 13 '18 at 21:05






  • 1





    Why don't just move findTimeSheets() into your subscription.

    – wannadream
    Nov 13 '18 at 21:26






  • 1





    No matter what, you will need to pipe them one by one, executing synchronously. This is the simplest way.

    – wannadream
    Nov 13 '18 at 21:34















0















I need to load some data from the server before further processing. So have I have this function:



async load() 
await this.reloadClients()
this.findTimeSheets();


findTimeSheets()
for (const client of this.clients)
console.log('Client: ' + client.lastName);




This function gets called when clicking a button on the component.



I need the function this.timeSheets to wait until this.reloadClients is done processing, so the data is ready. This is the the reload function, it should load a list of clients and store it in this.clients:



reloadClients() 
this.clientService.search(
page: '0',
query: 'assigner.id:' + this.selectedAssigner.id,
size: '250',
sort: '')
.subscribe((res: HttpResponse<Client>) => this.clients = res.body; this.init(res.body); , (res: HttpErrorResponse) => this.onError(res.message));



What happens now is that I need to click the button twice to get the correct data. So the loop in findTimeSheets() works after the second click (printing data to console).



My approach was to use await, but somehow this doesn't work.



As I'm new to Angular, I need some help resolving this.










share|improve this question

















  • 1





    return this.clientServiceSearch({...

    – Wainage
    Nov 13 '18 at 20:55











  • Won't help, as i'm subscribing to the result returned by the service.

    – Fish-Guts
    Nov 13 '18 at 21:04






  • 1





    It will. Makes the async call "wait". Otherwise it returns "undefined" before clientService gets to complete

    – Wainage
    Nov 13 '18 at 21:05






  • 1





    Why don't just move findTimeSheets() into your subscription.

    – wannadream
    Nov 13 '18 at 21:26






  • 1





    No matter what, you will need to pipe them one by one, executing synchronously. This is the simplest way.

    – wannadream
    Nov 13 '18 at 21:34













0












0








0








I need to load some data from the server before further processing. So have I have this function:



async load() 
await this.reloadClients()
this.findTimeSheets();


findTimeSheets()
for (const client of this.clients)
console.log('Client: ' + client.lastName);




This function gets called when clicking a button on the component.



I need the function this.timeSheets to wait until this.reloadClients is done processing, so the data is ready. This is the the reload function, it should load a list of clients and store it in this.clients:



reloadClients() 
this.clientService.search(
page: '0',
query: 'assigner.id:' + this.selectedAssigner.id,
size: '250',
sort: '')
.subscribe((res: HttpResponse<Client>) => this.clients = res.body; this.init(res.body); , (res: HttpErrorResponse) => this.onError(res.message));



What happens now is that I need to click the button twice to get the correct data. So the loop in findTimeSheets() works after the second click (printing data to console).



My approach was to use await, but somehow this doesn't work.



As I'm new to Angular, I need some help resolving this.










share|improve this question














I need to load some data from the server before further processing. So have I have this function:



async load() 
await this.reloadClients()
this.findTimeSheets();


findTimeSheets()
for (const client of this.clients)
console.log('Client: ' + client.lastName);




This function gets called when clicking a button on the component.



I need the function this.timeSheets to wait until this.reloadClients is done processing, so the data is ready. This is the the reload function, it should load a list of clients and store it in this.clients:



reloadClients() 
this.clientService.search(
page: '0',
query: 'assigner.id:' + this.selectedAssigner.id,
size: '250',
sort: '')
.subscribe((res: HttpResponse<Client>) => this.clients = res.body; this.init(res.body); , (res: HttpErrorResponse) => this.onError(res.message));



What happens now is that I need to click the button twice to get the correct data. So the loop in findTimeSheets() works after the second click (printing data to console).



My approach was to use await, but somehow this doesn't work.



As I'm new to Angular, I need some help resolving this.







angular typescript






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 13 '18 at 20:51









Fish-GutsFish-Guts

1039




1039







  • 1





    return this.clientServiceSearch({...

    – Wainage
    Nov 13 '18 at 20:55











  • Won't help, as i'm subscribing to the result returned by the service.

    – Fish-Guts
    Nov 13 '18 at 21:04






  • 1





    It will. Makes the async call "wait". Otherwise it returns "undefined" before clientService gets to complete

    – Wainage
    Nov 13 '18 at 21:05






  • 1





    Why don't just move findTimeSheets() into your subscription.

    – wannadream
    Nov 13 '18 at 21:26






  • 1





    No matter what, you will need to pipe them one by one, executing synchronously. This is the simplest way.

    – wannadream
    Nov 13 '18 at 21:34












  • 1





    return this.clientServiceSearch({...

    – Wainage
    Nov 13 '18 at 20:55











  • Won't help, as i'm subscribing to the result returned by the service.

    – Fish-Guts
    Nov 13 '18 at 21:04






  • 1





    It will. Makes the async call "wait". Otherwise it returns "undefined" before clientService gets to complete

    – Wainage
    Nov 13 '18 at 21:05






  • 1





    Why don't just move findTimeSheets() into your subscription.

    – wannadream
    Nov 13 '18 at 21:26






  • 1





    No matter what, you will need to pipe them one by one, executing synchronously. This is the simplest way.

    – wannadream
    Nov 13 '18 at 21:34







1




1





return this.clientServiceSearch({...

– Wainage
Nov 13 '18 at 20:55





return this.clientServiceSearch({...

– Wainage
Nov 13 '18 at 20:55













Won't help, as i'm subscribing to the result returned by the service.

– Fish-Guts
Nov 13 '18 at 21:04





Won't help, as i'm subscribing to the result returned by the service.

– Fish-Guts
Nov 13 '18 at 21:04




1




1





It will. Makes the async call "wait". Otherwise it returns "undefined" before clientService gets to complete

– Wainage
Nov 13 '18 at 21:05





It will. Makes the async call "wait". Otherwise it returns "undefined" before clientService gets to complete

– Wainage
Nov 13 '18 at 21:05




1




1





Why don't just move findTimeSheets() into your subscription.

– wannadream
Nov 13 '18 at 21:26





Why don't just move findTimeSheets() into your subscription.

– wannadream
Nov 13 '18 at 21:26




1




1





No matter what, you will need to pipe them one by one, executing synchronously. This is the simplest way.

– wannadream
Nov 13 '18 at 21:34





No matter what, you will need to pipe them one by one, executing synchronously. This is the simplest way.

– wannadream
Nov 13 '18 at 21:34












1 Answer
1






active

oldest

votes


















0














You have await this.reloadClients() However the function reloadClients is not async. So the await is useless.



Fix



Make reloadClients async e.g.



async reloadClients() 
await this.clientService.search(
page: '0',
query: 'assigner.id:' + this.selectedAssigner.id,
size: '250',
sort: '')
.subscribe((res: HttpResponse<Client>) => this.clients = res.body; this.init(res.body); , (res: HttpErrorResponse) => this.onError(res.message))
.toPromise();






share|improve this answer

























  • replace return with await

    – natqe
    Nov 14 '18 at 0:02











  • Done. Because, I can see your preference for future expansion 🌹

    – basarat
    Nov 14 '18 at 0:08











  • if you use the return keyword, you don't need the async keyword. but its a bit a lot tricky for the human eyes.

    – natqe
    Nov 14 '18 at 1:23











  • You cannot call toPromise on an RxJS subscription. Instead you could call toPromise first on the returned observable, and register the handlers that are now in subscribe in a then call.

    – Jeffrey Westerkamp
    Nov 14 '18 at 6:19










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53289319%2fdata-not-ready-when-needed%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














You have await this.reloadClients() However the function reloadClients is not async. So the await is useless.



Fix



Make reloadClients async e.g.



async reloadClients() 
await this.clientService.search(
page: '0',
query: 'assigner.id:' + this.selectedAssigner.id,
size: '250',
sort: '')
.subscribe((res: HttpResponse<Client>) => this.clients = res.body; this.init(res.body); , (res: HttpErrorResponse) => this.onError(res.message))
.toPromise();






share|improve this answer

























  • replace return with await

    – natqe
    Nov 14 '18 at 0:02











  • Done. Because, I can see your preference for future expansion 🌹

    – basarat
    Nov 14 '18 at 0:08











  • if you use the return keyword, you don't need the async keyword. but its a bit a lot tricky for the human eyes.

    – natqe
    Nov 14 '18 at 1:23











  • You cannot call toPromise on an RxJS subscription. Instead you could call toPromise first on the returned observable, and register the handlers that are now in subscribe in a then call.

    – Jeffrey Westerkamp
    Nov 14 '18 at 6:19















0














You have await this.reloadClients() However the function reloadClients is not async. So the await is useless.



Fix



Make reloadClients async e.g.



async reloadClients() 
await this.clientService.search(
page: '0',
query: 'assigner.id:' + this.selectedAssigner.id,
size: '250',
sort: '')
.subscribe((res: HttpResponse<Client>) => this.clients = res.body; this.init(res.body); , (res: HttpErrorResponse) => this.onError(res.message))
.toPromise();






share|improve this answer

























  • replace return with await

    – natqe
    Nov 14 '18 at 0:02











  • Done. Because, I can see your preference for future expansion 🌹

    – basarat
    Nov 14 '18 at 0:08











  • if you use the return keyword, you don't need the async keyword. but its a bit a lot tricky for the human eyes.

    – natqe
    Nov 14 '18 at 1:23











  • You cannot call toPromise on an RxJS subscription. Instead you could call toPromise first on the returned observable, and register the handlers that are now in subscribe in a then call.

    – Jeffrey Westerkamp
    Nov 14 '18 at 6:19













0












0








0







You have await this.reloadClients() However the function reloadClients is not async. So the await is useless.



Fix



Make reloadClients async e.g.



async reloadClients() 
await this.clientService.search(
page: '0',
query: 'assigner.id:' + this.selectedAssigner.id,
size: '250',
sort: '')
.subscribe((res: HttpResponse<Client>) => this.clients = res.body; this.init(res.body); , (res: HttpErrorResponse) => this.onError(res.message))
.toPromise();






share|improve this answer















You have await this.reloadClients() However the function reloadClients is not async. So the await is useless.



Fix



Make reloadClients async e.g.



async reloadClients() 
await this.clientService.search(
page: '0',
query: 'assigner.id:' + this.selectedAssigner.id,
size: '250',
sort: '')
.subscribe((res: HttpResponse<Client>) => this.clients = res.body; this.init(res.body); , (res: HttpErrorResponse) => this.onError(res.message))
.toPromise();







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 14 '18 at 0:07

























answered Nov 13 '18 at 23:50









basaratbasarat

138k25257364




138k25257364












  • replace return with await

    – natqe
    Nov 14 '18 at 0:02











  • Done. Because, I can see your preference for future expansion 🌹

    – basarat
    Nov 14 '18 at 0:08











  • if you use the return keyword, you don't need the async keyword. but its a bit a lot tricky for the human eyes.

    – natqe
    Nov 14 '18 at 1:23











  • You cannot call toPromise on an RxJS subscription. Instead you could call toPromise first on the returned observable, and register the handlers that are now in subscribe in a then call.

    – Jeffrey Westerkamp
    Nov 14 '18 at 6:19

















  • replace return with await

    – natqe
    Nov 14 '18 at 0:02











  • Done. Because, I can see your preference for future expansion 🌹

    – basarat
    Nov 14 '18 at 0:08











  • if you use the return keyword, you don't need the async keyword. but its a bit a lot tricky for the human eyes.

    – natqe
    Nov 14 '18 at 1:23











  • You cannot call toPromise on an RxJS subscription. Instead you could call toPromise first on the returned observable, and register the handlers that are now in subscribe in a then call.

    – Jeffrey Westerkamp
    Nov 14 '18 at 6:19
















replace return with await

– natqe
Nov 14 '18 at 0:02





replace return with await

– natqe
Nov 14 '18 at 0:02













Done. Because, I can see your preference for future expansion 🌹

– basarat
Nov 14 '18 at 0:08





Done. Because, I can see your preference for future expansion 🌹

– basarat
Nov 14 '18 at 0:08













if you use the return keyword, you don't need the async keyword. but its a bit a lot tricky for the human eyes.

– natqe
Nov 14 '18 at 1:23





if you use the return keyword, you don't need the async keyword. but its a bit a lot tricky for the human eyes.

– natqe
Nov 14 '18 at 1:23













You cannot call toPromise on an RxJS subscription. Instead you could call toPromise first on the returned observable, and register the handlers that are now in subscribe in a then call.

– Jeffrey Westerkamp
Nov 14 '18 at 6:19





You cannot call toPromise on an RxJS subscription. Instead you could call toPromise first on the returned observable, and register the handlers that are now in subscribe in a then call.

– Jeffrey Westerkamp
Nov 14 '18 at 6:19



















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53289319%2fdata-not-ready-when-needed%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







CKsLAWIaAbdXhSfV5gX
oXVc0AwSCLbeKWSX

Popular posts from this blog

Use pre created SQLite database for Android project in kotlin

Ruanda

Ondo