One thing I love about JavaScript is that there are many ways to accomplish the same task, one such example being creating functions. There are several patterns for functions; one of the last you see used is the new Function
method:
/* new Function(arg1, arg2 (...), body) */ const myFunction = new Function('users', 'salary', 'return users * salary');
What if you want to use this new Function
method to create an async function? You need to be a bit clever, and thanks to MDN, we have an answer:
// Shim for allowing async function creation via new Function const AsyncFunction = Object.getPrototypeOf(async function(){}).constructor; // Usage const fetchPage = new AsyncFunction("url", "return fetch(url);"); fetchPage("/").then(response => { ... });
The usage of
is super clever, as a native Object.getPrototypeOf(async function(){}).constructor
AsyncFunction
doesn’t exist. I don’t believe that I’ve ever used the new Function
pattern but that doesn’t mean you don’t! And now you can make them asynchronous!
LightFace: Facebook Lightbox for MooTools
One of the web components I’ve always loved has been Facebook’s modal dialog. This “lightbox” isn’t like others: no dark overlay, no obnoxious animating to size, and it doesn’t try to do “too much.” With Facebook’s dialog in mind, I’ve created LightFace: a Facebook lightbox…
Prevent Page Zooming in Mobile Browsers
Ever since I got my iPhone, I’ve been more agreeable in going places that my fiancee wants to go. It’s not because I have any interest in checking out women’s shoes, looking at flowers, or that type of stuff — it’s because my iPhone lets…