2017-10-10 60 views
0

這裏我正在製作一個天氣應用程序。首先,我要求並檢索前三行中的數據,並在控制檯中按照第四行進行打印。因爲node.js異步運行,所以console.log()函數在檢索數據之前運行。我該怎麼辦??如何在節點js中一行接一行地運行程序

代碼是:

//sending the request 
const APIMannager = require("./locationToWeather.js"); 
const apiMannager = new APIMannager(); 
const data = apiMannager.getLocation("pokhara"); 
console.log(data); //prints undefined 

//如何運行該程序後另一

+0

你應該使用'Promises'或'Callbacks'。或者如果你的'APIManager'有選項,你可以同時調用'getLocation'。 –

+0

你能舉個例子嗎? @peter – Abishek

回答

0

deasync一個行變爲異步函數轉換成同步,通過調用Node.js的具有阻擋機構實現JavaScript層的事件循環。