简体   繁体   中英

Azure function doesn't return response after async function is complete

I'm trying to develop a function in VS Code that takes an url as input and returns the response after processing is complete. However, when this function is run, it returns nothing. I tried testing similar code in Webstorm and and confirm that it console.logs the results just fine. I'm new to node and promises so not sure what I'm doing wrong.

Edit - added return keyword before driver.get as per the suggestion from @hellikiam. Also added a simple log statement to confirm that the results available yet not being returned in body.

var AxeBuilder = require('@axe-core/webdriverjs'),
    WebDriver = require('selenium-webdriver');
const chromedriver = require('chromedriver');
const chrome = require("selenium-webdriver/chrome");
const screen = {
    width: 640,
    height: 480
  };

chrome.setDefaultService(new chrome.ServiceBuilder(chromedriver.path).build());
var driver = new WebDriver.Builder()
    .forBrowser('chrome')
    .setChromeOptions(new chrome.Options().headless().windowSize(screen))
    .build();
module.exports = async function (context, req) {
    context.log('JavaScript HTTP trigger function processed a request.');

    const url = (req.query.url || (req.body && req.body.url));
    return driver.get(url).then(function () {
        new AxeBuilder(driver).analyze(function (err, results) {
            resultsJson = JSON.stringify(results);
            console.log(resultsJson)
                context.res = {
                    status: 200, /* Defaults to 200 */
                    body: resultsJson,
                    headers: {
                        'Content-Type': 'application/json'
                    }
                };

                if (err) {
                    // Handle error somehow
                    }           
            });
        });
   
    context.done();
}

you didn't returned anything from exported module.

return driver.get(url).then(function () {//add return statement in here.
        new AxeBuilder(driver).analyze(function (err, results) {
            resultsJson = JSON.stringify(results);
                context.res = {
                    status: 200, /* Defaults to 200 */
                    body: resultsJson,
                    headers: {
                        'Content-Type': 'application/json'
                    }
                };

                if (err) {
                    // Handle error somehow
                    }           
        });
        });

I found the other problem. What you want to return is inside callback function. You should wrap the callback with promise to get the result outside of the callback. Try this out brother.


    return new Promise((resolve, reject) =>{
       driver.get(url).then(function () {//add return statement in here.
            new AxeBuilder(driver).analyze(function (err, results) {
                resultsJson = JSON.stringify(results);
                    context.res = {
                        status: 200, /* Defaults to 200 */
                        body: resultsJson,
                        headers: {
                            'Content-Type': 'application/json'
                        }
                    };
                    resolve(resultsJson)//Any value you want to return
                    if (err) reject(err)
            });
       });
    
    })

Calling this function outside of module with await statement

const foo = require('./my/path/foo')

(async()=>{
    const params = 'my param'
    const bar = await foo(params)
    console.log(bar)
})()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM