Skip to content

Instantly share code, notes, and snippets.

View adambene's full-sized avatar

Adam Bene adambene

View GitHub Profile
@adambene
adambene / index.html
Created December 5, 2016 18:42
Custom File Upload Button With Pure CSS
<div class="upload-btn-wrapper">
<button class="btn">Upload a file</button>
<input type="file" name="myfile" />
</div>
@adambene
adambene / AlternatingArray.js
Created March 6, 2018 14:58
Iterate through an array in an alternating fashion in JavaScript
class AlternatingArray extends Array {
constructor(...args) {
super(...args);
}
*[Symbol.iterator](){
for (let i = 0; i < this.length; i += 2) {
yield this[i];
}
for (let i = 1; i < this.length; i += 2) {
yield this[i];
@adambene
adambene / generator-iterator-for-of.js
Created March 7, 2018 15:58
Generator, iterator, for...of in JavaScript
function* naturals() {
let i = 1;
while (true) {
// it can count to infinity
yield i++;
}
}
for (let i of naturals()) {
// count only to 5
@adambene
adambene / merge-two-arrays.js
Last active March 7, 2018 19:27
Merge two arrays in JavaScript
// merge two arrays a and b
const merge = (a, b) => a.reduce(
(acc, curr, i) => [
...acc, // copy all previous elements
curr, // current element of a
b[i] // current element of b
],
[] // initial value
);
@adambene
adambene / delays-with-coroutines.js
Last active March 8, 2018 00:10
Delays With Coroutines in JavaScript
function* delays() {
let a = yield delay(800, "Hello, I'm an");
console.log(a);
let b = yield delay(400, "async coroutine!");
console.log(b);
}
// call it
coroutine()(delays());
@adambene
adambene / delay-with-a-value.js
Created March 8, 2018 00:11
Delay With a Value in JavaScript
const delay = (ms, result) =>
new Promise(resolve => setTimeout(() => resolve(result), ms));
@adambene
adambene / delays-with-promise-chaining.js
Last active March 8, 2018 17:06
Delays With Promise Chaining in JavaScript
function delays() {
return delay(800, "Hello, I'm in a").then(a => {
console.log(a);
return delay(400, "Promise!");
}).then(b => {
console.log(b);
});
}
// call it
@adambene
adambene / delays-with-callback-hell.js
Created March 8, 2018 17:09
Delays With Callback Hell in JavaScript
const delay = (ms, result, cb) => setTimeout(() => cb(result), ms);
function delays() {
delay(800, "Hello, I'm in a", a => {
console.log(a);
delay(400, "callback!", b => {
console.log(b);
});
});
}
@adambene
adambene / coroutine-runner.js
Last active March 8, 2018 17:23
Coroutine runner in JavaScript
const coroutine = nextValue => iterator => {
const { done, value } = iterator.next(nextValue);
if (done) {
return;
}
if (value.constructor === Promise) {
value.then(promiseValue => {
coroutine(promiseValue)(iterator);
@adambene
adambene / delays-with-async-await.js
Last active April 4, 2018 00:55
Delays With Async/await in JavaScript
async function delays() {
let a = await delay(800, "Hello, I'm in an");
console.log(a);
let b = await delay(400, "async function!");
console.log(b);
}
delays();