Skip to content

Instantly share code, notes, and snippets.

View batrudinych's full-sized avatar

batrudinych batrudinych

View GitHub Profile
@batrudinych
batrudinych / eosjs-tx-signing-example.js
Last active December 10, 2020 09:27
sign transaction with eosjs (signle and double authorization)
// npm i eosjs node-fetch
// node eosjs-tx-singing-example.js
'use strict'
const { Api, JsonRpc } = require('eosjs')
const { JsSignatureProvider } = require('eosjs/dist/eosjs-jssig')
const fetch = require('node-fetch')
const { TextDecoder, TextEncoder } = require('util')
const privateKeys = ['_INSERT_PRIVATE_KEY_']
@batrudinych
batrudinych / send-blob.js
Created March 10, 2019 14:28
Send a blob (which may be a file) from client app
// Blob may contain something else, check https://developer.mozilla.org/en-US/docs/Web/API/XMLHttpRequest/send#Syntax
// Keep in mind this requires preflight
const blob = new Blob(['this is a test'], { type: 'application/x-www-form-urlencoded'})
const xhr = new XMLHttpRequest();
xhr.open("POST", 'http://localhost:8080/files', true);
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xhr.onreadystatechange = function() {
@batrudinych
batrudinych / [parallel-promises]-take3-subtake1-part1.js
Created January 14, 2019 10:58
Running async operations in parallel and harvesting the results. Concurrency limit applied correctly
async function take3subtake1part1() {
const concurrencyLimit = 5;
// Enhance arguments array to have an index of the argument at hand
const argsCopy = [].concat(listOfArguments.map((val, ind) => ({ val, ind })));
const result = new Array(listOfArguments.length);
const promises = new Array(concurrencyLimit).fill(Promise.resolve());
// Recursively chain the next Promise to the currently executed Promise
function chainNext(p) {
if (argsCopy.length) {
const arg = argsCopy.shift();
@batrudinych
batrudinych / [parallel-promises]-take3-subtake1-part0.js
Created January 14, 2019 10:58
Running async operations in parallel. Concurrency limit applied correctly
async function take3subtake1part0() {
const concurrencyLimit = 5;
const argsCopy = listOfArguments.slice();
const promises = new Array(concurrencyLimit).fill(Promise.resolve());
// Recursively chain the next Promise to the currently executed Promise
function chainNext(p) {
if (argsCopy.length) {
const arg = argsCopy.shift();
return p.then(() => {
const operationPromise = asyncOperation(arg);
@batrudinych
batrudinych / [parallel-promises]-take3-subtake0.js
Created January 14, 2019 10:58
Running async operations in parallel and harvesting the results. Concurrency limit applied incorrectly
async function take3subtake0() {
const concurrencyLimit = 5;
let results = [];
const batchesCount = Math.ceil(numberOfOperations / concurrencyLimit);
// Running Promises in parallel in batches
for (let i = 0; i < batchesCount; i++) {
const batchStart = i * concurrencyLimit;
const batchArguments = listOfArguments.slice(batchStart, batchStart + concurrencyLimit);
const batchPromises = batchArguments.map(asyncOperation);
@batrudinych
batrudinych / [parallel-promises]-take2.js
Created January 14, 2019 10:58
Running async operations in parallel and harvesting the results
async function take2() {
// Running Promises in parallel
const listOfPromises = listOfArguments.map(asyncOperation);
// Harvesting
return await Promise.all(listOfPromises);
}
@batrudinych
batrudinych / [parallel-promises]-take1.js
Created January 14, 2019 10:58
Running async operations in parallel and harvesting the results
async function take1() {
// Running Promises in parallel
const listOfPromises = listOfArguments.map(asyncOperation);
// Harvesting
const results = [];
for (const promise of listOfPromises) {
const index = await promise;
results.push(index);
}
return results;
@batrudinych
batrudinych / [parallel-promises]-take0.js
Created January 14, 2019 10:58
Running async operations sequentially and harvesting the results
async function take0() {
// Harvesting
const results = [];
for (const argument of listOfArguments) {
const index = await asyncOperation(argument);
results.push(index);
}
return results;
}
@batrudinych
batrudinych / [parallel-promises]-common.js
Last active February 4, 2019 13:29
List of variables and function shared between invocations
// Used to watch amount of Promises executing in a single moment of time
let counter = 0;
let interval;
// Overall amount of operations
const numberOfOperations = 25;
// Arguments per operation
const listOfArguments = [];
// Delays per operation to fake async request
const listOfDelays = [];
@batrudinych
batrudinych / parallel-promises.js
Created January 14, 2019 10:19
Run promises in parallel, applying the concurrency limit
**
* Run Promises in parallel with a concurrency limit
* @param args - List of arguments to be passed to 'fn'
* @param fn - A function to apply to each argument. MUST return a Promise
* @param concurrency - Allowed amount of Promises to be run in parallel
* @returns {Promise} - A Promise, which eventually resolved with a list of results.
* Order corresponds to the list of arguments.
*/
function mapPromises(args, fn, concurrency = 1) {
if (!args.length) return Promise.resolve([]);