Skip to content

Instantly share code, notes, and snippets.

@Deepak13245
Last active July 7, 2021 19:22
Show Gist options
  • Save Deepak13245/129ecdbb72c9f0f81827eb421260a8da to your computer and use it in GitHub Desktop.
Save Deepak13245/129ecdbb72c9f0f81827eb421260a8da to your computer and use it in GitHub Desktop.
Few js utilities
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