import axios from "axios"; const cheerio = require("cheerio"); async function sleep(millis) { return new Promise((resolve) => setTimeout(resolve, millis)); } const getData = async (customer_no, year, month) => { const container = []; const PromiseArr = []; try { //loop through the customer_no and scrape for (let i = 0; i < customer_no.length; i++) { let url = `https://dpdc.org.bd/service/ebill?btyp=current&year=${year}&month=${month}&cno=${customer_no[i]}&email=`; PromiseArr.push(axios.get(url)); } //end of for loop Promise.all(PromiseArr).then(function (response) { const $ = cheerio.load(response.data); /* scraping logic here */ container.push(data);//push the scrape data await sleep(5000);//wait for 5s }); //end of promise all } catch (error) { console.log(error); // output to netlify function log return { statusCode: 400, body: JSON.stringify({ msg: err.message }), // Could be a custom message or object i.e. JSON.stringify(err) }; } console.log(container); return container; }; export async function handler(event, context) { /* event.queryParameter */ try { const data = await getData(customer, year, month); return { statusCode: 200, body: JSON.stringify({ data: data }), }; } catch (err) { console.log(err); // output to netlify function log return { statusCode: 500, body: JSON.stringify({ msg: err.message }), // Could be a custom message or object i.e. JSON.stringify(err) }; } }