0

I have a node project, I want to populate a var with the return value of a function that make async stuff :

index.js

const search = require( './search.js' );

(async () => {
    try {
        var test =  await search.searchMU('test');
        console.log(test);
    } catch (e) {

    }
})();

Search.js

const puppeteer = require( 'puppeteer' );
exports.searchMU = function( searchInput ) {
    const fullUrl = url + excludes + type + display + search + searchInput;
    puppeteer.launch().then( async browser => {
      const page = await browser.newPage();
      await page.goto( fullUrl );
      var html = await page.content();
      await browser.close();
      return html;
    } );
}

Output :

undefined

2
  • 3
    searchMU doesn't return anything, add a return statement before puppeteer.launch..... Commented Sep 20, 2019 at 18:23
  • stackoverflow.com/questions/47789093/… Commented Sep 20, 2019 at 18:26

1 Answer 1

1

You're using await search.searchMU() but searchMU does not return a promise. Also, why use explicit promise chaining style (.then(...)) if you can use await everywhere?

exports.searchMU = async function( searchInput ) {
    const fullUrl = url + excludes + type + display + search + searchInput;
    const browser = await puppeteer.launch()
    const page = await browser.newPage();
    await page.goto( fullUrl );
    var html = await page.content();
    await browser.close();
    return html;
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you I did like the doc : pptr.dev/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.