I'm writing an API that receives a request & then runs a fairly complex function on Puppeteer however I don't want the API to wait for the function to finish executing before throwing a success response as it's only a submit API?
Here's the current flow:
const createOrder = async (req, res) => {
try {
console.log(req.body);
let params = await parser.parseStringPromise(req.body.parameters);
device_imei = params.PARAMETERS.IMEI;
//config
axios.defaults.withCredentials = true;
axios.defaults.timeout = 15000;
userProfile = faker.entity.user();
recaptcha_solution = await get_captcha_solution();
page = await browser.newPage();
page.on('response', handle_response);
await page.goto('https://www.website.com', {
waitUntil: 'networkidle0',
});
//etc......
} catch (error) {
if(page) {
await page.close();
}
return res.send(error_response(error.message));
}
});
app.post('/api/index.php', async function (req, res) {
switch(req.body.action) {
case "placeimeiorder":
return await createOrder(req, res);
default:
return res.send(error_response('Invalid Action'));
}
});
How could I have it just execute the function & then return a instant JSON response while the script runs in the background?