Skip to content

Instantly share code, notes, and snippets.

@SnitchRUS66
Created April 21, 2020 19:59
Show Gist options
  • Save SnitchRUS66/84229233f0852e32d6f74660ba881cad to your computer and use it in GitHub Desktop.
Save SnitchRUS66/84229233f0852e32d6f74660ba881cad to your computer and use it in GitHub Desktop.
const vm = require('vm');
const fs = require('fs');
let callback;
function testQuery(callback) {
setTimeout(function () { callback(100500); }, 2000);
}
function testResult(result) {
callback(result); // В данном случае callback это функция переданная при инициализации
}
async function mySuperPuperFunction(filePath, cb) {
callback = cb;
let queryResult;
await new Promise((resolve) => {
testQuery((data) => {
queryResult = data;
resolve();
});
});
const sandbox = {
testQuery: () => {
return queryResult;
},
testResult: (some) => {
testResult(some);
}
}
const code = fs.readFileSync(filePath).toString();
vm.createContext(sandbox);
vm.runInContext(code, sandbox);
}
module.exports = mySuperPuperFunction;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment