Skip to content

Instantly share code, notes, and snippets.

View dnafication's full-sized avatar
🏔️
chill

Dina Basumatary dnafication

🏔️
chill
View GitHub Profile
@dnafication
dnafication / esm-package.md
Created September 16, 2022 07:31 — forked from sindresorhus/esm-package.md
Pure ESM package

Pure ESM package

The package linked to from here is now pure ESM. It cannot be require()'d from CommonJS.

This means you have the following choices:

  1. Use ESM yourself. (preferred)
    Use import foo from 'foo' instead of const foo = require('foo') to import the package. You also need to put "type": "module" in your package.json and more. Follow the below guide.
  2. If the package is used in an async context, you could use await import(…) from CommonJS instead of require(…).
  3. Stay on the existing version of the package until you can move to ESM.
@dnafication
dnafication / p-queue-simple-example.js
Created July 21, 2022 08:52
p-queue is a full fledged promise queue. It has a lot of advanced use cases. I provide a basic example here.
const { promisify } = require("util");
const sleep = promisify(setTimeout);
const doSomething = async (arg) => {
console.log(`doing something with ${arg}`);
await sleep(1000);
if (arg === 5) throw Error("bummer!");
console.log(`done something with ${arg}`);
return arg * 10;
@dnafication
dnafication / p-limit-example.js
Created July 21, 2022 08:40
p-limit allows you to call Promise.all but with a concurrency limit. The code example below executes the async function with a concurrency limit of 3
const { promisify } = require("util");
const sleep = promisify(setTimeout);
const doSomething = async (arg) => {
console.log(`doing something with ${arg}`);
await sleep(1000);
if (arg === 5) throw Error("bummer!");
console.log(`done something with ${arg}`);
return arg * 10;
@dnafication
dnafication / modern-async-queue-example.js
Created July 21, 2022 08:22
Example for a task queue in node.js using library called `modern-async`. Error handling considered. API looks better than `fastq`
const { Queue, sleep } = require("modern-async");
const queue = new Queue(3); // create a queue with concurrency 3
async function main() {
const array = Array.from(Array(30).keys()); // an array of 100 numbers from 0 to 99
const promises = [];
for (const i of array) {
promises.push(
@dnafication
dnafication / try-fastq.js
Created July 21, 2022 08:20
fastq example in nodejs. Task queue to run tasks concurrently with concurrency specified. Handle error occurred in worker fn.
const fastq = require("fastq");
// sleep function
function sleep(ms) {
return new Promise((resolve) => setTimeout(resolve, ms));
}
// worker function
async function worker(arg) {
console.log(`working on task ${arg}`);
@dnafication
dnafication / ApolloIdempotent.ts
Created March 11, 2022 04:05 — forked from jmagnuss/ApolloIdempotent.ts
Empty plugin sample for Apollo Server
import {
ApolloServerPlugin,
GraphQLRequestContext,
GraphQLRequestListener,
GraphQLServiceContext,
GraphQLResponse,
} from 'apollo-server-plugin-base';
import {
ApolloError,
AuthenticationError,
@dnafication
dnafication / s3-iterator.ts
Last active January 14, 2021 02:31
Iterator pattern for s3 objects
import * as AWS from 'aws-sdk';
import {GetObjectRequest, ListObjectsV2Request} from 'aws-sdk/clients/s3';
// basic aws configuration stuff
AWS.config.update({
region: 'ap-southeast-2',
});
// using this interface to store all the keys
interface S3Key {
@dnafication
dnafication / .vimrc
Created October 12, 2020 04:54
my vim settings
" URL: http://vim.wikia.com/wiki/Example_vimrc
" Authors: http://vim.wikia.com/wiki/Vim_on_Freenode
" Description: A minimal, but feature rich, example .vimrc. If you are a
" newbie, basing your first .vimrc on this file is a good choice.
" If you're a more advanced user, building your own .vimrc based
" on this file is still a good idea.
"------------------------------------------------------------
" Features
"
function timeout(timeInMs, wontDo) {
return new Promise((resolve, reject) => {
if (wontDo === true) {
reject(Error("I wont do!"));
}
setTimeout(resolve(`timeout finished ${timeInMs}`), timeInMs);
});
}
async function A() {