recreate eval function in java script

  eval, javascript, node.js

How can I recreate the eval function in JavaScript? I want to do this because I am not making function exactly same as the original eval, even if I do function eval2(s) { dosomething(); eval(something + s + somethingelse); dosomething(); }. It runs in another scope and not the local scope.

What I want to do:

if(Number(process.version.match(/(d+.d+)/)[0]) < 7.6) {  // no async await support
    var { async, await } = require('asyncawait');  // npm module that allows me to use ES7 async await on node.js 6.0
} else {
    var async = function(func) { 
        return (async function() {
            return func.apply(this, arguments);
        });
    };
    var await = (function RECREATED_EVAL_FUNCTION(promise) {
        eval('await promise');
    });
}

(async(function() {
    await(new Promise(/* something */));
}))();

but it creates a new scope and I get a syntax error.

Please don’t ask why I need old node.js support. I want my script to work on older verisons too.

Source: Ask Javascript Questions

LEAVE A COMMENT