Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

how to create a dynamic function in javascript

// for async
const AsyncFunction = Object.getPrototypeOf(async function(){}).constructor;
// assume someAsyncCall is a call to another async func we await for that returns 1 (just make this simpler)
const func = new AsyncFunction('arg1', 'arg2', 'return arg1 * arg2 * await someAsyncCall();');
// now use the function, assuming we are in an async function for the following to work
await func(2,2); // => 4


// for normal non-async functions it's simpler just use the Function constructor
const func = new Function('arg1', 'arg2', 'return arg1 * arg2;');
// now use the function
func(2,2); // => 4
Comment

PREVIOUS NEXT
Code Example
Javascript :: javascript get first element of array 
Javascript :: react composition 
Javascript :: .keys() array 
Javascript :: bootstrap and masonry 
Javascript :: angular add ellipsis to template string 
Javascript :: delete js 
Javascript :: print stuff in console javascript 
Javascript :: Angular Mat-Table with Dynamic Columns generate and Data should be populated in horizontal way 
Javascript :: how to add jquery to an html css and javascript project 
Javascript :: javascript array destructuring 
Javascript :: getdefaultmiddleware redux toolkit deprecated 
Javascript :: sprintf js 
Javascript :: ajax call to load a page on scrolling 
Javascript :: js animations 
Javascript :: webpack vue global variable 
Javascript :: object literals and array literals in javascript 
Javascript :: reset event listener javascript 
Javascript :: angular two datepickers 
Javascript :: jquery repeat event on click 
Javascript :: javascript regular expression 
Javascript :: how ot send user agent in nodejs https header 
Javascript :: object assign in javascript 
Javascript :: setattribute 
Javascript :: jquery get search parameter 
Javascript :: computed property names 
Javascript :: sequilze REACTJS 
Javascript :: jquery check valid link 
Javascript :: jquery remove all alerts 
Javascript :: javascript addeventlistener pass parameters 
Javascript :: js background color 
ADD CONTENT
Topic
Content
Source link
Name
2+6 =