My code does not wait to return the result from an API call.
I need help on fixing the async/await syntax, can somebody help me?
const axios = require("axios");
async function getRate() {
const response = await axios.get('https://open.er-api.com/v6/latest/USD');
print(response.data.rates.EUR)
return response.data.rates.EUR;
};
return await getRate()
That’s the result I get from the terminal:
2023-10-13 12:17:12.242 PopClip[9569:350202] [INFO] main: PopClip main() called
PopClip JavaScript Test Harness (apiCall.js)
require(axios): Loading /Applications/PopClip.app/Contents/Resources/js_bundles/axios.js.lzfse
XHR: Starting GET request for url 'https://open.er-api.com/v6/latest/USD' with responseType '' and headers:
Accept: application/json, text/plain, */*
XHR: status 200 (no error), headers:
content-type: application/json
access-control-allow-origin: *
last-modified: Fri, 13 Oct 2023 14:11:03 GMT
nel: {"success_fraction":0,"report_to":"cf-nel","max_age":604800}
content-encoding: br
server: cloudflare
age: 171
report-to: {"endpoints":[{"url":"https:\/\/a.nel.cloudflare.com\/report\/v3?s=iyiL7BhXufwpO%2Fm%2BuTIhlWkt8xyuaKYVodvWqKy0pUQnSIaYH0Erv3Amq%2F7Q9MFzjvaCd55xgY%2BBrUf4G5txiaTiM85MaIig17TjYviOxoT91BMz9qJjxVIZeExbIed9zdZ%2BJj8zET78EnSwoQ%3D%3D"}],"group":"cf-nel","max_age":604800}
cf-cache-status: HIT
cache-control: public, max-age=3600
date: Fri, 13 Oct 2023 14:54:19 GMT
x-content-type-options: NOSNIFF
cf-ray: 81586acbff9b2622-GIG
vary: Accept-Encoding
x-frame-options: SAMEORIGIN
XHR: response length: 2908, mimetype: application/json (text), charset: (null), response:
{"result":"success","provider":"https://www.exchangerate-api.com","documentation":"https://www.exchangerate-api.com/docs/free","terms_of_use":"https://www.exchangerate-api.com/terms","time_last_update_unix":1697155351,"time_last_update_utc":"Fri, 13 Oct 2023 00:02:31 +0000","time_next_update_unix":1697243021,"time_next_update_utc":"Sat, 14 Oct 2023 00:23:41 +0000","time_eol_unix":0,"base_code":"USD","rates":{"USD":1,"AED":3.6725,"AFN":75.550889,"ALL":99.742392,"AMD":397.055033,"ANG":1.79,"AOA":835.619699,"ARS":350.03,"AUD":1.577862,"AWG":1.79,"AZN":1.702093,"BAM":1.851479,"BBD":2,"BDT":110.287519,"BGN":1.850435,"BHD":0.376,"BIF":2831.034802,"BMD":1,"BND":1.366971,"BOB":6.92028,"BRL":5.048046,"BSD":1,"BTN":83.294779,"BWP":13.716816,"BYN":3.293651,"BZD":2,"CAD":1.365413,"CDF":2479.851446,"CHF":0.905719,"CLP":925.311747,"CNY":7.305164,"COP":4230.677317,"CRC":534.190708,"CUP":24,"CVE":104.381965,"CZK":23.266639,"DJF":177.721,"DKK":7.06893,"DOP":56.779044,"DZD":137.369415,"EGP":30.90266,"ERN":15,"ETB":55.792174,"EUR":0.94647,"FJD":2.266273,"FKP":0.818756,"FOK":7.062342,"GBP":0.818761,"GEL":2.69016,"GGP":0.818756,"GHS":11.905216,"GIP":0.818756,"GMD":65.826637,"GNF":8591.219145,"GTQ":7.841103,"GYD":209.364355,"HKD":7.822635,"HNL":24.659914,"HRK":7.132507,"HTG":134.675747,"HUF":365.736498,"IDR":15707.797765,"ILS":3.963366,"IMP":0.818756,"INR":83.294887,"IQD":1308.757634,"IRR":41921.710555,"ISK":137.930564,"JEP":0.818756,"JMD":155.249347,"JOD":0.709,"JPY":149.567557,"KES":149.0905,"KGS":88.758913,"KHR":4126.029195,"KID":1.575229,"KMF":465.719498,"KRW":1345.213848,"KWD":0.309237,"KYD":0.833333,"KZT":475.732016,"LAK":20521.561238,"LBP":15000,"LKR":323.260212,"LRD":189.740852,"LSL":18.977655,"LYD":4.887352,"MAD":10.240551,"MDL":18.14819,"MGA":4526.359547,"MKD":58.15111,"MMK":2099.599851,"MNT":3464.000023,"MOP":8.057551,"MRU":38.4406,"MUR":44.188424,"MVR":15.435283,"MWK":1165.118935,"MXN":17.954467,"MYR":4.714381,"MZN":63.89939,"NAD":18.977655,"NGN":766.16673,"NIO":36.589402,"NOK":10.941421,"NPR":133.271646,"NZD":1.684667,"OMR":0.384497,"PAB":1,"PEN":3.834274,"PGK":3.693217,"PHP":56.712268,"PKR":279.49264,"PLN":4.288736,"PYG":7333.738937,"QAR":3.64,"RON":4.68142,"RSD":110.390702,"RUB":97.600283,"RWF":1246.823377,"SAR":3.75,"SBD":8.515787,"SCR":13.632928,"SDG":559.94472,"SEK":10.969705,"SGD":1.366468,"SHP":0.818756,"SLE":22.593073,"SLL":22593.073057,"SOS":572.101212,"SRD":38.553905,"SSP":1020.951271,"STN":23.192837,"SYP":12955.364871,"SZL":18.977655,"THB":36.33494,"TJS":10.93555,"TMT":3.500833,"TND":3.173806,"TOP":2.351888,"TRY":27.772441,"TTD":6.768636,"TVD":1.575229,"TWD":32.174167,"TZS":2508.768888,"UAH":36.412402,"UGX":3759.39122,"UYU":39.920175,"UZS":12269.438563,"VES":34.9001,"VND":24439.07751,"VUV":121.829619,"WST":2.763914,"XAF":620.95933,"XCD":2.7,"XDR":0.759568,"XOF":620.95933,"XPF":112.965208,"YER":250.398742,"ZAR":18.98816,"ZMW":21.535855,"ZWL":5643.037096}}
0.94647
Done: success: <no output text>