I want to pass a variable defined inside request-promise to a variable out of scope. I thought I may pass the variable using a return statement inside the request function, but the request function always returns the http response (in this case as HTML output).
My code:
require('dotenv').config();
const jsdom = require("jsdom");
const { JSDOM } = jsdom;
var request = require('request-promise');
var get = function(arg1, arg2, arg3, arg4) {
request(`https://www.googleapis.com/customsearch/v1?key=${process.env.API_KEY}&cx=${process.env.CX}&q=${arg1}`, async function(err, res, body) {
var obj = JSON.parse(body);
var url = obj.items[0].formattedUrl;
var part = await request(`${url}/staffel-${arg2}/episode-${arg3}`, function(err, res, body) {
const dom = new JSDOM(`${body}`);
var part = dom.window.document.getElementsByClassName(arg4)[0];
return part;
});
console.log(part);
});
};
Furthermore, I'd like to pass the output to the get-function using return statements. Is there any way to do so?
Edit: From my point of view, this is not a duplicate of this question. The request returns a value, but it's not the value I want to return as explained above.