Created
April 21, 2020 19:59
-
-
Save SnitchRUS66/84229233f0852e32d6f74660ba881cad to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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