Skip to content

Instantly share code, notes, and snippets.

@yaasita
Created May 29, 2019 13:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yaasita/a0bcf6d1329c525b9d5cb584ed125519 to your computer and use it in GitHub Desktop.
Save yaasita/a0bcf6d1329c525b9d5cb584ed125519 to your computer and use it in GitHub Desktop.
"use strict";
const express = require("express");
const util = require("util");
const exec = util.promisify(require("child_process").exec);
const app = express();
app.get("/", async (req, res) => {
const wait_time = parseInt(req.query.wait) || 150;
let state = false;
cmd(wait_time, () => {
state = true;
});
{
let i=0;
while(true){
i++;
await sleep(1);
res.write(`wait ${i} seconds\n`);
if (state){
break;
}
}
}
res.end();
});
async function cmd(wait_time, callback){
console.log(`wait_time: ${wait_time}`);
const { stdout, stderr } = await exec(`sleep ${wait_time};echo ok`);
console.log(`stdout: ${stdout}`);
console.log(`stderr: ${stderr}`);
callback();
}
function sleep(time){
return new Promise((resolv,reject) => {
setTimeout(() => {
resolv();
}, time * 1000);
});
}
const port = process.env.PORT || 8080;
app.listen(port, () => {
console.log("listening on port", port);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment