I'm trying to make a function that gets data from CoinMarketCap API. For some reason the function kept getting called last of the script. Below is the entire code I've written.
const axios = require('axios');
let promise = new Promise(async (resolve, reject) => {
let response = null;
try {
response = await axios.get('https://sandbox-api.coinmarketcap.com/v2/cryptocurrency/quotes/latest', {
headers: {
'X-CMC_PRO_API_KEY': 'b54bcf4d-1bca-4e8e-9a24-22ff2c3d462c',
},
params: {
'id' : 1,
}
});
} catch(ex) {
// error
response = null;
console.log(ex);
reject(ex);
}
if (response) {
// success
const json = response.data;
resolve(json);
}
});
console.log("HI1");
promise.then((data) => {
console.log(data);
}).catch(err => {
console.log(err);
});
console.log("HI2");
When ever I run it it will result like this:
HI1
HI2
{dataFromPromiseFunction}