I use the request module as follows:

 var request = require("request"); var url = "http://www.google.com"; //... request(url, function(error, response, body) { var step1 = body.replace(/<.+?>/g, ''); //... console.log(step3); 

Data is collected, but I cannot match the original request with the answer. I thought that it was necessary to include the contents of the url variable in the data inside the anonymous function. But how to do it, I do not understand.

What can be done to display url -> response pairs?

    1 answer 1

    You should request a wrap in promise:

     const _request = url =>{ return (new Promise(function(resolve, reject) { request(url,(err,res,body)=>resolve(body)); })) } 

    Here you can read about promises https://learn.javascript.ru/promise

    And then use async / await:

     var urls = [ 'http://www.google.com/0', 'http://www.google.com/1', ] (async ()=>{ let list = []; for(let url of urls){ let body = await _request(url); list.push([url,body]) } console.log(list); return list; })(); 

    Here about async / await https://javascript.info/async-await

    The for loop can be replaced with the following code:

     let list = await Promise.all(urls.map(async url=>{ let body = await _request(url); return [url,body]; })); 

    Then requests will go at once, asynchronously and Promise.all will wait for all requests to be executed.

    • Thank you very much! I will get acquainted with promises. Heard, but not yet used. - Danny