I need to call the result(array from a function) Called outside the function
My Code look likes this
import axios from 'axios'
var cheats
(() => {
axios.get('api/cheats/getcheaternames')
.then((response) => {
cheats = response.data
console.log(cheats, 'yeah')//results[Good, Bad]
return cheats
})
.catch((error) => {
console.log(error, 'err')
})
})()
const it = [
{
name: ...
}
]
This is how I hardcode the result,
const it = [
{
name: 'Good'
},
{
name: 'Bad'
},
{
name: 'Handsome'
}
]
Any idea to dynamically populate the it variable?
Update
I have no enough reputation to answer my own question, here it is how I refactor my code to make it work
import axios from 'axios'
var cheats
var it = []
let myFirstPromise = new Promise((resolve, reject) => {
setTimeout(() => {
resolve(axios.get('api/cheats/getcheaternames')
.then((response) => {
cheats = response.data
return cheats
})
.catch((error) => {
console.log(error, 'err')
}))
}, 250)
})
myFirstPromise.then((successMessage) => {
// for (var i = 0; i < successMessage.length; i++) {
// it.push({name: i})
// }
for (const value of successMessage) {
it.push({name: value})
}
})
export default {
items: [
{
name: 'Dashboard',
url: '/dashboard',
icon: 'icon-speedometer',
badge: {
variant: 'primary',
text: 'Cheaters'
}
},
{
title: true,
name: 'Elements',
class: '',
wrapper: {
element: '',
attributes: {}
}
},
{
name: 'Cheats',
url: '/components',
icon: 'icon-puzzle',
children: it
},
{
name: 'Angular Version',
url: 'https://angular.herokuapp.com/',
icon: 'icon-cloud-download',
class: 'mt-auto',
variant: 'success'
}
]
}
Any idea to make this code better is appreciated. Am I missing something or am I doing right with this code?