In the below script I need to implement a Promise for listSpaces()
as it uses axios
to fetch data externally.
Reading this I don't understand
If the condition is met, the Promise will be resolved, otherwise it will be rejected
In my case listSpaces()
can either return an empty array or an array with elements.
Question
What condition is it he speaks of? And how can that be tied to my listSpaces()
?
#!/usr/bin/env node
const yargs = require('yargs');
const listSpaces = require('./functions/cmdListSpaces');
yargs.command({
command: 'list-spaces',
describe: 'List spaces',
type: 'boolean',
handler(argv) {
const a = listSpaces();
a.forEach((v) => {
console.log(v);
});
},
}).argv;
listSpaces()
const toml = require('./toml');
const config = toml('config/kibana.toml');
const axios = require('axios');
module.exports = async () => {
const r = await axios({
method: 'get',
url: `${config.Url}api/spaces/space`,
auth: {
username: config.Username,
password: config.Password,
},
headers: { 'kbn-xsrf': true },
})
.then(response => {
let a = [];
response.data.forEach((v) => {
a.push(v.id);
});
return a;
})
.catch(error => {
return error.response.data.statusCode;
});
};