Skip to content

Instantly share code, notes, and snippets.

View vkarpov15's full-sized avatar

Valeri Karpov vkarpov15

View GitHub Profile
@vkarpov15
vkarpov15 / text.md
Last active May 16, 2022 20:26
Caching API Requests With Long-Lived Workflows in Temporal

There is no time limit on Temporal Workflow Executions. You can write a Workflow that runs forever, storing some state and responding to Signals and Queries that come in, as long as you remember to Continue As New. One neat use case for long-lived Workflows is caching API requests.

For example, suppose you want to display prices in different currencies based on cached exchange rates. Exchange rate APIs are often expensive for high volumes of requests, so caching can be worthwhile as long as stale data isn't a problem for your use case. You can create a single Temporal Workflow that makes 1 API request per day to get the latest exchange rates for a set of currencies, and stores the most recent result, with no explicit database calls or cron jobs. In this blog post, I'll describe how you can write an API caching Workflow for the [moneyconvert.net API](https://moneyconvert.net

@vkarpov15
vkarpov15 / socketreporter.js
Created June 29, 2019 15:57
Example of reporting on progress using an async generator function and websockets
const WebSocket = require('ws');
// Core solver logic, stubbed out for example
async function* slow() {
yield 'starting...';
await new Promise(resolve => setTimeout(resolve, 10 * 1000));
// Use `yield` to report on progress
yield 'loading data...';
await new Promise(resolve => setTimeout(resolve, 10 * 1000));
@vkarpov15
vkarpov15 / slowtrain.js
Created May 7, 2019 15:59
MongoDB cursor slow train
const mongodb = require('mongodb')
run().catch(error => console.log(error));
async function run() {
const db = await mongodb.MongoClient.
connect('mongodb://localhost:27017/test', {
useNewUrlParser: true,
poolSize: 1 // Only 1 operation can run at a time
}).
@vkarpov15
vkarpov15 / index.js
Created April 25, 2019 23:51
Hello, Imports with `package.json`
import test from './test.js';
test();
@vkarpov15
vkarpov15 / remove.js
Created November 27, 2018 18:57
Mute intercom
[document.querySelector('#intercom-frame'), document.querySelector('#intercom-container')].forEach(el => { el.parentNode.removeChild(el); });
@vkarpov15
vkarpov15 / geo.js
Created July 18, 2018 13:49
Example of querying a GeoJSON feature collection in MongoDB
db.test.drop();
db.test.insertOne({
name: 'Denver',
location: {
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"properties": {},
"geometry": {
@vkarpov15
vkarpov15 / async.js
Created June 26, 2018 17:55
Async example
let i = 0;
run();
setInterval(() => console.log(i), 10);
async function run() {
++i;
await new Promise(resolve => setTimeout(resolve, 15));
++i;
@vkarpov15
vkarpov15 / promise.js
Last active October 22, 2021 17:22
Simple Promises/A+ Compliant Promise
const assert = (v, err) => {
if (!v) {
throw err;
}
};
let counter = 0;
class Promise {
constructor(executor) {
@vkarpov15
vkarpov15 / promise2.js
Created April 5, 2018 13:57
Write Your Own Node.js Promise Library from Scratch, Part 2
class MyPromise {
constructor(executor) {
if (typeof executor !== 'function') {
throw new Error('Executor must be a function');
}
// Internal state. `$state` is the state of the promise, and `$chained` is
// an array of the functions we need to call once this promise is settled.
this.$state = 'PENDING';
this.$chained = [];
@vkarpov15
vkarpov15 / promise1.js
Created April 5, 2018 12:38
Write Your Own Node.js Promise Library from Scratch, Part 1
class MyPromise {
constructor(executor) {
if (typeof executor !== 'function') {
throw new Error('Executor must be a function');
}
// Internal state. `$state` is the state of the promise, and `$chained` is
// an array of the functions we need to call once this promise is settled.
this.$state = 'PENDING';
this.$chained = [];