javascript - Not a Constructor with async function - Stack Overflow

I am trying to make a constructor function in JavaScript, and this constructor should be asynchronous,

I am trying to make a constructor function in JavaScript, and this constructor should be asynchronous, cause I am using Phantom JS module to scrape the data,so that's why I have to use asynchronous function to scrap data through Phantom JS with Node JS.

Below is my code,

const phantom = require('phantom');
async function InitScrap() {

  var MAIN_URL = "/",
      //Phantom JS Variables
      instance = await phantom.create(),
      page = await instance.createPage();


  // Load the Basic Page First      
  this.loadPage = async function() {
    console.log("Loading Please wait...");
    var status = await page.open(MAIN_URL);
    if (status == "success") {
      page.render("new.png");
      console.log("Site has been loaded");
    }
  }

}

var s = new InitScrap();
s.loadPage()

// module.exports = InitScrap();

I am trying to make a constructor function in JavaScript, and this constructor should be asynchronous, cause I am using Phantom JS module to scrape the data,so that's why I have to use asynchronous function to scrap data through Phantom JS with Node JS.

Below is my code,

const phantom = require('phantom');
async function InitScrap() {

  var MAIN_URL = "https://www.google./",
      //Phantom JS Variables
      instance = await phantom.create(),
      page = await instance.createPage();


  // Load the Basic Page First      
  this.loadPage = async function() {
    console.log("Loading Please wait...");
    var status = await page.open(MAIN_URL);
    if (status == "success") {
      page.render("new.png");
      console.log("Site has been loaded");
    }
  }

}

var s = new InitScrap();
s.loadPage()

// module.exports = InitScrap();

But when I run this code it says, InitScrap() is not a constructor, am I missing something ?

Share Improve this question edited Aug 5, 2018 at 12:22 Luca Kiebel 10.1k7 gold badges32 silver badges46 bronze badges asked Aug 5, 2018 at 12:16 Nadeem AhmadNadeem Ahmad 7453 gold badges17 silver badges45 bronze badges 4
  • Because it's a function? – Ric Commented Aug 5, 2018 at 12:19
  • 3 async functions cannot be constructors – Aritra Chakraborty Commented Aug 5, 2018 at 12:21
  • This is how we make constructor functions @Ric – Nadeem Ahmad Commented Aug 5, 2018 at 12:22
  • A constructor should not do anything asynchronous – Bergi Commented Aug 5, 2018 at 12:32
Add a ment  | 

2 Answers 2

Reset to default 3

Constructor functions are functions that return an Object of the type defined in the function, like this "Person" class from MDN you cited:

function Person(name) {
  this.name = name;
  this.greeting = function() {
    alert('Hi! I\'m ' + this.name + '.');
  };
}

It returns an Object with a name and a greeting function when used with the new keyword.

When you use the async keyword, you can await Promises in the function but it also converts that function into a promise generator, meaning it will return a Promise, not an Object, that's why it can't be a constructor.

Refer here for more details: Async/Await Class Constructor

But to summarize,

async functions cannot be constructors.

function func(){
    this.foo = 'bar'
}

const f = new func();
console.log(f.foo);

This will work but async function func() { .. } will not work

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745512890a4630851.html

相关推荐

  • javascript - Not a Constructor with async function - Stack Overflow

    I am trying to make a constructor function in JavaScript, and this constructor should be asynchronous,

    1月前
    50

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信