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
// 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_'] |
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
// 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() { |
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 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(); |
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 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); |
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 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); |
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 take2() { | |
// Running Promises in parallel | |
const listOfPromises = listOfArguments.map(asyncOperation); | |
// Harvesting | |
return await Promise.all(listOfPromises); | |
} |
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 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; |
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 take0() { | |
// Harvesting | |
const results = []; | |
for (const argument of listOfArguments) { | |
const index = await asyncOperation(argument); | |
results.push(index); | |
} | |
return results; | |
} |
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
// 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 = []; |
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
** | |
* 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([]); |
NewerOlder