Alex Alex - 1 year ago 65
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");




function httpafterlogin(url, cookie, day) {

headers: {
'content-type': 'text/html',
'Cookie': cookie
url: url,

function(error, response, body) {

var $ = cheerio.load(body);

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

console.log(resturanttables, "nside");

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


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






function httplogin(username, password, urls, day) {{
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;

cookie = cookie.substring(0, cookie.indexOf(';'));

// httpafterlogin(''+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("", "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 Source

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) {
  } else {

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

httplogin("zeke", "coys", url, day).then(function () {
}).catch(function (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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download