Skip to content

Instantly share code, notes, and snippets.

@blackChef
blackChef / runPromiseFnInOrder.js
Created March 24, 2020 10:33
Execute async functions one by one, and collect result after all functions are finished
// [() => Promise] => Promise
const runPromiseFnInOrder = async function(fns) {
const ret = [];
for (const fn of fns) {
const r = await fn();
ret.push(r);
}
return Promise.resolve(ret);
};
const item = function(id) {
return `
<input type="text" name="${id}"/>
`;
};
const list = function(ids) {
return `
<div>
${ids.map(item)}
@blackChef
blackChef / AnimateScrollTo.js
Created September 3, 2019 14:53
AnimateScrollTo
const nativeScrollTo = function(to) {
window.scrollTo({
top: to,
left: 0,
behavior: 'smooth',
});
};
const customScrollTo = function(to, duration = 1000) {
@blackChef
blackChef / renderExample.js
Created June 20, 2017 08:47
render example
data = {
name: 'John',
age: 20,
items: [
{ id: 0, name: 'a' },
{ id: 1, name: 'b' },
{ id: 2, name: 'c' },
]
};
@blackChef
blackChef / debounceWithQueue.js
Created April 27, 2017 09:48
debounce with queue
let debounceWithQueue = function(fn, wait = 0) {
let timeoutId;
let argsQueue = [];
return function(arg) {
argsQueue = argsQueue.concat(arg);
if (timeoutId !== undefined) {
clearTimeout(timeoutId);
}