Function Citywithlowtemp Dbconnection Return New Promise Function Resolve Var Returnarray Q43814246
function cityWithLowTemp(dbConnection){
return new Promise(function(resolve){
var returnArray = [];
for (var i = 0; i<dbConnection.length; i++){
let apiKey=’*****’;
const params = {
url:`http://api.openweathermap.org/data/2.5/forecast?zip=${dbConnection[i]},us&units=imperial&APPID=${apiKey}`,
json: true,
headers: {‘Content-type’: ‘application/json’},
};
req.get(params, function(error, response, body){
if(error){
console.log(error);
}
else{
let message = body.list[0].main.temp_min;
//Do the algorithm stuff here
returnArray.push(message);
}
});
resolve(returnArray);
}
});
}
test();
async function test(){
var dbConnection = await connectToDB();
console.log(dbConnection);
var coldTempArray = await cityWithLowTemp(dbConnection);
console.log(coldTempArray);
}
dbConnection that is passed onto the cityWithLowTemp function isjust an array of zip codes. I’m trying to figure out where I shouldput the resolve(returnArray). The code is correctly pushing themessage into returnArray but it either resolves with one item inthe array or none at all. Where can I put the resolve where it willcorrectly return the entire array. This is in javascript.
Expert Answer
Answer to function cityWithLowTemp(dbConnection){ return new Promise(function(resolve){ var returnArray = []; for (var i = 0; i…
OR