【发布时间】:2020-11-21 02:20:31
【问题描述】:
是否可以在函数中等待异步函数的响应,而不使整个函数异步,ala await 没有异步,或类似的东西? 一个例子:
const axios = require('axios');
const converter = require('number-to-words');
const formatter = new Intl.NumberFormat('en-US', {
style: 'currency',
currency: 'USD',
});
function dataFetch() {
//fetches current exchange rate of NOK to USD
let data = await axios.get('https://api.exchangeratesapi.io/latest?base=NOK');
//fetches current value of Norwegian Oil fund
let res = await axios.get('https://www.nbim.no/LiveNavHandler/Current.ashx');
//extracts value of Norwegian Oil Fund from response data, and converts it to USD in real time.
let dolla = (res.data.Value.replace(/\s/g, '') * data.data.rates.USD).toFixed(2);
//Converts Basic number to number with commas, and a dollar sign.
let val = formatter.format(dolla);
//Generates word format of number(i.e. one hundred instead of 100)
let str = converter.toWords(dolla);
//Generates word format of cents.
let cents = converter.toWords(dolla.split('.')[1].substring(0, 2));
//Returns JSON of commaed value and full word string
return {num: val, word: `${str} dollars and ${cents} cents`};
}
console.log(dataFetch().val);
我想确保 dataFetch() 返回的值是从 get 请求接收到的数据的处理版本,而不是一个承诺,或者 null 以便console.log() 打印出基金的价值美元,而不是 undefined、null 或其他东西。
几乎可以肯定有一个简单的解决方案,但我似乎找不到它,而且它已经破坏了我一直在研究的代码。
更新:更新以添加问题的上下文。我为我稍微不清楚的 cmets 道歉。
【问题讨论】:
-
不,这是不可能的(没有涉及阻塞子进程的大量黑客攻击)。您需要学习如何异步编程。此外,您永远都不想阻塞 nodejs 服务器,因为它会破坏您的服务器可扩展性。如果您在函数中异步获取结果,则永远不会直接从函数返回该值,因为该函数将在您获得该值之前返回。因此,您将通过 Promise、回调或事件返回异步值。这就是 Javascript 中异步 I/O 的工作原理。
-
如果您展示了您要解决的问题的更大背景,那么这里的人们可能会帮助您展示如何使用异步 I/O 正确编码。
-
感谢您的回复,jfriend00,我试图用更多的实际代码来充实我的问题,以表明我的目标是什么。我提前为稍微草率的 cmets 道歉,因为我不是最擅长按照我在脑海中听到的方式格式化我的 cmets。
标签: node.js async-await