Created
November 5, 2021 15:39
-
-
Save runjak/bdbb7ae31fc8813144d9a5778ca801d6 to your computer and use it in GitHub Desktop.
Experiments towards a custom context from hell
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const sleep = (t) => new Promise(resolve => setTimeout(resolve, t)); | |
// must be function & cannot be strict mode | |
function withContext(context, next) { | |
return next(); | |
} | |
const getContext = () => withContext.arguments[0]; | |
const logContext = (label) => function () { | |
console.log(`${label} has context:`, getContext()); | |
}; | |
const contextTest1 = () => { | |
withContext({foo: 1}, logContext('contextTest1')); | |
}; | |
// contextTest1(); | |
const twice = (label, t) => async () => { | |
const log = logContext(label) | |
await sleep(t); | |
log(); | |
await sleep(t); | |
log(); | |
await sleep(t); | |
}; | |
const contextTest2 = async () => { | |
await withContext({foo :2}, twice('contextTest2', 500)); | |
}; | |
// contextTest2(); | |
const contextTest3 = () => { | |
const log1 = logContext('contextTest3.1'); | |
const log2 = logContext('contextTest3.2'); | |
withContext({foo: 3.1},() => { | |
log1(); | |
withContext({foo: 3.2}, log2); | |
log1(); | |
}); | |
}; | |
// contextTest3(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment