Last active
July 7, 2021 19:22
-
-
Save Deepak13245/129ecdbb72c9f0f81827eb421260a8da to your computer and use it in GitHub Desktop.
Few js utilities
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
async function measure(promise) { | |
const start = Date.now(); | |
try { | |
const result = await promise; | |
return result; | |
} catch(e) { | |
throw e; | |
} finally { | |
const time = Date.now() - start; | |
console.log(`Time taken ${time} ms`); | |
} | |
} | |
async function logPromise(promise) { | |
try{ | |
console.log(await promise); | |
}catch(e){ | |
console.error(e); | |
} | |
} | |
function chunk(list, size) { | |
const out = []; | |
for( let i = 0; i < list.length ; i += size ) { | |
out.push(list.slice(i, i + size)); | |
} | |
return out; | |
} | |
function range(size, offset = 0) { | |
return [...Array(size).keys()].map(x => x + offset); | |
} | |
function promisify(fn) { | |
return (...args) => { | |
return new Promise((resolve, reject) => { | |
fn(...args, (err, data) => { | |
if(err) { | |
return reject(err); | |
} | |
resolve(data); | |
}); | |
}); | |
} | |
} | |
const noOp = () => {}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment