Javascript
Notes
-
Make a dynamic chain of promises
Use
arr.reduce((c, d) => c.then(() => fn(d)), Promise.resolve()).catch(error);to convert
const arr = [d1, d2, d3, ..., dn]; const fn = async (d) => { /* returns a promise */ };into
Promise.resolve().then(() => fn(d1)).then(() => fn(d2)).then(() => fn(d3))...then(() => fn(dn)).catch(error)-
In newer versions:
for (const d of arr) await fn(d);
-
-
Convert string to num shorthand
const numStr = "1.23"; const numVal = +numStr; -
Get integer part of a fraction (like Math.floor)
const fraction = 1.234; const intPart = ~~fraction; // 1 const intPart2 = fraction << 0; // 1The
~operator is 2’s complement