Adapter
call
Given a key and a set of arguments, call them when given a context. Primarily useful in composition.
Use a closure to call a stored key with stored arguments.
const call = (key, ...args) => context => context[key](...args);
+ }
30 seconds of code Curated collection of useful JavaScript snippets that you can understand in 30 seconds or less.
Adapter
call
Given a key and a set of arguments, call them when given a context. Primarily useful in composition.
Use a closure to call a stored key with stored arguments.
const call = (key, ...args) => context => context[key](...args);
Promise.resolve([1, 2, 3])
.then(call('map', x => 2 * x))
.then(console.log); //[ 2, 4, 6 ]
@@ -422,10 +422,7 @@ elementIsVisibleInViewport(el, true); // true // (partially visible)
asLink ? (window.location.href = url) : window.location.replace(url);
redirect('https://google.com');
runAsync
Runs a function in a separate thread by using a Web Worker, allowing long running functions to not block the UI.
Create a new Worker using a Blob object URL, the contents of which should be the stringified version of the supplied function. Immediately post the return value of calling the function back. Return a promise, listening for onmessage and onerror events and resolving the data posted back from the worker, or throwing an error.
const runAsync = fn => {
- const blob = `
- var fn = ${fn.toString()};
- this.postMessage(fn());
- `;
+ const blob = `var fn = ${fn.toString()}; postMessage(fn());`;
const worker = new Worker(
URL.createObjectURL(new Blob([blob]), {
type: 'application/javascript; charset=utf-8'
@@ -442,9 +439,9 @@ elementIsVisibleInViewport(el, true); // true // (partially visible)
};
const longRunningFunction = () => {
let result = 0;
- for (var i = 0; i < 1000; i++) {
- for (var j = 0; j < 700; j++) {
- for (var k = 0; k < 300; k++) {
+ for (let i = 0; i < 1000; i++) {
+ for (let j = 0; j < 700; j++) {
+ for (let k = 0; k < 300; k++) {
result = result + i + j + k;
}
}
diff --git a/snippets/runAsync.md b/snippets/runAsync.md
index 8ab34943e..e2573a061 100644
--- a/snippets/runAsync.md
+++ b/snippets/runAsync.md
@@ -8,10 +8,7 @@ Return a promise, listening for `onmessage` and `onerror` events and resolving t
```js
const runAsync = fn => {
- const blob = `
- var fn = ${fn.toString()};
- this.postMessage(fn());
- `;
+ const blob = `var fn = ${fn.toString()}; postMessage(fn());`;
const worker = new Worker(
URL.createObjectURL(new Blob([blob]), {
type: 'application/javascript; charset=utf-8'
@@ -31,9 +28,9 @@ const runAsync = fn => {
```js
const longRunningFunction = () => {
let result = 0;
- for (var i = 0; i < 1000; i++) {
- for (var j = 0; j < 700; j++) {
- for (var k = 0; k < 300; k++) {
+ for (let i = 0; i < 1000; i++) {
+ for (let j = 0; j < 700; j++) {
+ for (let k = 0; k < 300; k++) {
result = result + i + j + k;
}
}
diff --git a/static-parts/index-start.html b/static-parts/index-start.html
index fd7122025..b46cd2dee 100644
--- a/static-parts/index-start.html
+++ b/static-parts/index-start.html
@@ -81,7 +81,7 @@
}
});
-
+
30 seconds of code
Curated collection of useful JavaScript snippets that you can understand in 30 seconds or less.