Alex Alex - 17 days ago 9
Javascript Question

How to solve promise issue?

I am new to Promise concepts and trying to wrap my head around but now I`` am confused here

const request = require("request");
const cheerio = require("cheerio");
const XMLHttpRequest = require("xmlhttprequest").XMLHttpRequest;
var url = require("url");


module.exports = {

resturant: resturant,

};


var resturanttables = [];

function resturant(url, day) {

return new Promise(function(resolve, reject) {
request(url, function(error, response, html) {

if (error) {
return reject(error);

} else if (response.statusCode !== 200) {

return reject("Something is wrong! CINEMA")

}
httplogin("zeke", "coys", url, day);
console.log(resturanttables, "i am here");

resolve(resturanttables);

});



});
}

function httpafterlogin(url, cookie, day) {

request.get({
headers: {
'content-type': 'text/html',
'Cookie': cookie
},
url: url,
},

function(error, response, body) {



console.log(day)
var $ = cheerio.load(body);

if (day === "Friday") {
$(".WordSection2 p span ").each(function(li) {
// console.log(day, $(this).text())
resturanttables.push($(this).text());

console.log(resturanttables, "nside");
});

} else if (day === "Saturday") {
$(".WordSection4 p span").each(function(li) {

resturanttables.push($(this).text())
});

} else {
$(".WordSection6 p span").each(function(li) {

resturanttables.push($(this).text())


});

}

});

}

function httplogin(username, password, urls, day) {

request.post({
headers: {
'content-type': 'application/x-www-form-urlencoded'

},
url: urls,
form: {
"username": username,
"password": password


}
}, function(error, response, body) {
var cookie = response.caseless.dict['set-cookie'][0];
var location = response;

console.log(response.statusCode);
cookie = cookie.substring(0, cookie.indexOf(';'));

// httpafterlogin('http://vhost3.lnu.se:20080/dinner/'+response.headers.location, cookie);
var newurls = url.resolve(urls, response.headers.location)
httpafterlogin(newurls, cookie, day);

// console.log(response.headers, "jdjdjjdjdjjdjdjdjjdjjdjdj")

});

}


and then I call the function

loadPage.resturant("http://vhost3.lnu.se:20080/dinner/login", "Friday").then(function(data) {
console.log(data, "did it work now ")
})


the problem is that it returns the empty array. But when i tried to check and console.log in the afterlogin function and i could see that the array was actually filled, but that code runs after the promise has been resolved.
IN SHORT: how can I bound the resolve in restaurant promise not to send the data until the login function is completed?

in other words how can i get the filled array with information from afterlogin funtion?

rsp rsp
Answer

If you don't want the promise to get resolved before the login is completed then you will either have to make your httplogin function take a callback and run it like this:

httplogin("zeke", "coys", url, day, function (err) {
  if (err) {
    reject(err);
  } else {
    resolve(resturanttables);
  }
});

or make it return a promise and run it for example like this:

httplogin("zeke", "coys", url, day).then(function () {
  resolve(resturanttables);
}).catch(function (err) {
  reject(err);
});

There are more ways to do it with promises but this is the simplest way.

Either way you have to make your httplogin function signal its completion by either calling the callback that it takes as an argument or resolving the promise that it returns.