递归调用api(axios/javascript)
Recursively call api (axios / javascript)
我正在尝试用递归多次 API 调用的结果填充一个数组。我对 ES6 和递归的东西感到头晕,可能需要一些帮助。
这是我目前的代码,只有 returns "promise" :
getAllEmployees: function() {
let allEmployees = []; // this array will contain all employees
let pageNumber = 1; // start with page 1
const getThoseEmployees = function(pageNumber) {
return axios.get(rest_url + 'users/?per_page=50&page=' + pageNumber, {
headers: {
'X-WP-Nonce': WPsettings.nonce
},
}).then(response => {
// add the employees of this response to the array
allEmployees = allEmployees.concat(response.data);
pageNumber++;
if (response.headers['x-wp-total'] > allEmployees.length) {
// do me again...
return getThoseEmployees(pageNumber);
} else {
// this was the last page, return the collected contacts
return allEmployees;
}
});
}
return getThoseEmployees();
},
// after "created" in vue
this.allEmployees = this.getAllEmployees(); // returns "promise"
要访问从 Promise 解析的值,您必须使用 Promise 的 .then
方法。因此,this.allEmployees = this.getAllEmployees();
不是这个赋值,而是从 getAllEmployees
访问已解析的值,如下所示:
this.getAllEmployees()
.then(employees => {
console.log(employees); // make sure this is what you need
this.allEmployees = employees;
});
编辑:回复评论。
你的函数getAllEmployees
returns的值getThoseEmployees
,这是一个承诺。因为 allEmployees
,最终返回时,在 .then
的匿名函数内部,该值将始终在 getThoseEmployees
.
返回的承诺内部
// This functions return value is a promise
const getData = () => {
return axios.get('some-url')
.then(data => {
// Anything returned inside of a .then will be
// resolved and can only be accessed with another .then.
// Using .then itself will return another promise object,
// which is why promises can be chained.
return formatThisData(data);
});
};
为了从 getData
访问我想要的格式化数据,我必须从 promise 访问已解析的数据。
getData()
.then(formattedData => {
// formattedData is what was returned from inside the .then above
});
我正在尝试用递归多次 API 调用的结果填充一个数组。我对 ES6 和递归的东西感到头晕,可能需要一些帮助。
这是我目前的代码,只有 returns "promise" :
getAllEmployees: function() {
let allEmployees = []; // this array will contain all employees
let pageNumber = 1; // start with page 1
const getThoseEmployees = function(pageNumber) {
return axios.get(rest_url + 'users/?per_page=50&page=' + pageNumber, {
headers: {
'X-WP-Nonce': WPsettings.nonce
},
}).then(response => {
// add the employees of this response to the array
allEmployees = allEmployees.concat(response.data);
pageNumber++;
if (response.headers['x-wp-total'] > allEmployees.length) {
// do me again...
return getThoseEmployees(pageNumber);
} else {
// this was the last page, return the collected contacts
return allEmployees;
}
});
}
return getThoseEmployees();
},
// after "created" in vue
this.allEmployees = this.getAllEmployees(); // returns "promise"
要访问从 Promise 解析的值,您必须使用 Promise 的 .then
方法。因此,this.allEmployees = this.getAllEmployees();
不是这个赋值,而是从 getAllEmployees
访问已解析的值,如下所示:
this.getAllEmployees()
.then(employees => {
console.log(employees); // make sure this is what you need
this.allEmployees = employees;
});
编辑:回复评论。
你的函数getAllEmployees
returns的值getThoseEmployees
,这是一个承诺。因为 allEmployees
,最终返回时,在 .then
的匿名函数内部,该值将始终在 getThoseEmployees
.
// This functions return value is a promise
const getData = () => {
return axios.get('some-url')
.then(data => {
// Anything returned inside of a .then will be
// resolved and can only be accessed with another .then.
// Using .then itself will return another promise object,
// which is why promises can be chained.
return formatThisData(data);
});
};
为了从 getData
访问我想要的格式化数据,我必须从 promise 访问已解析的数据。
getData()
.then(formattedData => {
// formattedData is what was returned from inside the .then above
});