R
R
Ruslan Absalyamov2018-11-27 13:37:46
Vue.js
Ruslan Absalyamov, 2018-11-27 13:37:46

Am I making asynchronous requests right?

Tell me if I'm doing it right, it seems to me that the code is repeated and you can somehow write it differently

created(){
            setTimeout(() => {
                let employeeApi = new restApi(this.$props.data_table);
                employeeApi.list().then(res => {
                    this.items = res.data;
                });
            });
            setTimeout(() => {
                let employeeColumn = new restApi(this.$props.data_columns);
                employeeColumn.list().then(res => {
                    this.columns = res.data;
                })
            });
        }

That is, I want requests to the server to be sent simultaneously and not in turn

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Anton Anton, 2018-11-27
@rusline18

You need something like this:

let employeeApi = new restApi(this.$props.data_table);
                let employeeColumn = new restApi(this.$props.data_columns);
                let arr = [employeeApi.list(), employeeColumn.list()];
                Promise.All(arr).then(res=> {
                  this.items = res[0].data;
                	this.columns = res[1].data;
                });

In general, there is bluebirdjs.com/docs/api-reference.html which has .spread, .delay, .promisify and a bunch of other syntactic goodies

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question