We know that the JavaScript language allows you to
Funtion()
Generating function,async
Can functions be generated in this way?
What I like about JavaScript is that there are many ways to accomplish the same function in the end, and creating a function is an example. There are several ways to create functions, one of which may be the least you seenew Function
method:
/* new Function(arg1, arg2 (...), body) */
const myFunction = new Function('users', 'salary', 'return users * salary');
If you want to usenew Function
Method creationasync
How to write a function? You have to be smart, thanksMDNAnd found an answer:
//Creating asynchronous functions with new methods
const AsyncFunction = Object.getPrototypeOf(async function(){}).constructor;
//Use
const fetchPage = new AsyncFunction("url", "return await fetch(url);");
fetchPage("/").then(response => { ... });
useObject.getPrototypeOf(async function(){}).constructor
This method is very fresh, because the native method is very simpleAsyncFunction
non-existent. Now you can use it to create asynchronous functions.
link
- Original link-https://davidwalsh.name/async…
- In the original
method
– https://davidwalsh.name/new-f…
First address of translationhttps://github.com/liuvigongzuoshi/blog