我正在尝试从异步函数获取 http get 响应。但是在函数中显示值但返回值未定义。
甚至承诺不是未定义的值
请在下面找到代码
'use strict';
const express = require('express');
var request = require('request');
var https = require('https');
async function getCurrencies() {
let response;
try {
var getOptions = {
url: 'http://localhost:3000/api/currency',
method: 'GET',
headers: {
'Content-Type': 'application/json'
},
json:true
};
await request(getOptions, function (err, res, body) {
if (res && (res.statusCode === 200 || res.statusCode === 201)) {
console.log(' response ', res.body.rates.INR);
return res.body;
} else {
console.log('error body ', body);
}
});
} catch (error) {
console.log(" error pulling ", error);
process.exit();
}
}
var tt = getCurrencies().then(function(value) {
console.log(' tt values ',value);
}
);
下面是日志
tt values undefined
response 64.945