javascript - 在 Node.js 中完成另一个异步函数之前发生的异步函数
问题描述
现在,老实说,我真的不知道 node.js 中的 async/await,但我被卡住了 :(
这是我的代码
async function main(){
var pokemonID = getRandomPokemon();
getJSON(pokemonID)
.then(data => {getPokemonInfo(data)})
const tempData = require('./temp_data.json');
await down.load(tempData.pokemonImg)
var tweetText = "Today's Pokemon of the day is " + tempData.pokemonName + ", which is a " + tempData.pokemonType + " type pokemon"
await tweet.tweeter(tweetText)
}
main()
我没有收到错误,但第二个异步函数在第一个异步函数之前执行。
解决方案
您await
还可以添加getJSON
async function main(){
var pokemonID = getRandomPokemon();
await getJSON(pokemonID) //set await
.then(data => {getPokemonInfo(data)})
const tempData = require('./temp_data.json');
await down.load(tempData.pokemonImg)
var tweetText = "Today's Pokemon of the day is " + tempData.pokemonName + ", which is a " + tempData.pokemonType + " type pokemon"
await tweet.tweeter(tweetText)
}
推荐阅读
- reactjs - onClick 不会改变 REACTJS 中的 useState
- twilio - 使用 Twilio for WhatsApp 在 Microsoft Botframework 中发布机器人
- python - 为什么我在运行 loaddata 时会收到此错误?
- reactjs - 对所有表格页面的所有输入字段进行 React AntDesign 表单验证
- constructor - 二维数组的复制构造函数
- javascript - 如何根据Javascript中的另一个对象过滤对象数组
- flutter - 检查颤振中的双重扩展
- button - 带按钮的 ON/OFF 开关(LTSpice 和电子测试)
- sql-server - 使用 SQL 一起使用 count 和 case
- statistics - 二阶统计分析或发现混杂因素数据科学