showing results for - "asynchronous function using function constructor"
Emilia
14 Nov 2017
1const AsyncFunction = Object.getPrototypeOf(async function(){}).constructor;
2// assume someAsyncCall is a call to another async func we await for that returns 1 (just make this simpler)
3const func = new AsyncFunction('arg1', 'arg2', 'return arg1 * arg2 * await someAsyncCall();');
4// now use the function, assuming we are in an async function for the following to work
5await func(2,2); // => 4
6
7// for normal non-async functions it's simpler just use the Function constructor
8const func = new Function('arg1', 'arg2', 'return arg1 * arg2;');
9// now use the function
10func(2,2); // => 4