K
K
Konstantin2021-01-28 15:57:01
Angular
Konstantin, 2021-01-28 15:57:01

Getting data asynchronously?

The service has a variable: private layers = [];
Which is filled statically in the service constructor:

constructor() {
    this.layers.push({id: 1});
}


Also in the service there is an asynchronous method:
public load() {
    return this.http.load();
}


How to add the result of an async function to this.layers.push()?

The problem is that in the component where I use the service - I want to get completely filled this.layers.from the service from asynchronous filling and synchronous.

Simple example:

let a = [];

a.push(1);

setTimeout(() => a.push(4), 5000);

console.log(a);

// В компоненте получить полный a[1, 4]

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question