Home Function enters then before request completed
Reply: 2

Function enters then before request completed

Stephen Paul Samynathan
1#
Stephen Paul Samynathan Published in 2018-02-14 07:39:19Z

My async function enters then before request is completed. Shouldn't Then part of the code executes only after the async function is completed? How to make the function call only when all the function has finished executing?

app.js

var request_test = require('./request_test');

baseUrl = "https://github.com";
promiseFunction().then((result)=>{
    console.log("complete")
});

var promiseFunction = async function promiseFunction() {
    request_test.request_test(baseUrl);
}

request_test.js

var request = require('request');
var cheerio = require('cheerio');

var request_test = function check(baseUrl) {
    console.log("baseUrl:" + baseUrl)
     var options = {
        url: baseUrl
     };
     request(options, function (error, response, html) {
        if (!error) {
           console.log("no error");
        }else{
        console.log("else js");
        console.log(error);
        }
      });
 }

module.exports = {
    request_test: request_test
};

codtex
2#
codtex Reply to 2018-02-14 08:21:31Z

In order to use then() you need to return a promise. So here is an example of the good old style promise chain, simply return promise from request_test and once you resolve or reject it, then() will be called:

promiseFunction().then((result) => {
  console.log("complete");
});

function promiseFunction() {
  return request_test();
}

function request_test() {
  return new Promise(function(resolve, reject) {
    setTimeout(function() {
      console.log("no error");
      resolve();
    }, 1000);
  });
}

Or maybe use the modern approach - async method to await call function that returns promise.

promiseFunction();

async function promiseFunction() {
  await request_test();
  console.log('complete');
}

function request_test() {
  return new Promise(function(resolve, reject) {
    setTimeout(function() {
      console.log("no error");
      resolve();
    }, 1000);
  });
}

Your issue is coming from var request_test = function check(baseUrl) { ... inside this function you are not returning promise, you are even returning nothing :)

Mario F
3#
Mario F Reply to 2018-02-14 08:32:12Z

if you are using async I would go ahead and use the await/async syntax. Also the package request does not return a promise, you have an alternative with request-promise-native. The promise should be the return value of your helper function. It could look like this:

var request_test = require('./request_test');
var baseUrl = "https://github.com";

var promiseFunction = async function () {
  var result = await request_test.request_test(baseUrl);
  console.log("complete");
}

promiseFunction();

and the module:

var request = require('request-promise-native');
var cheerio = require('cheerio');

var request_test = function check(baseUrl) {
  console.log("baseUrl:" + baseUrl)
  var options = {
    url: baseUrl
  };

 return request(options).then(function (error, response, html) {
   if (!error) {
     console.log("no error");
   } else{
     console.log("else js");
     console.log(error);
   }
 });
}

module.exports = {
  request_test: request_test
};
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.416119 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO