Iterator
A convenient way to read data sent to your Sequence is by using iterator pattern. Iterator functions provide a standardized way to loop through elements, making the code more readable and allowing it for consistent iteration patterns across different data structures.
Asynchronous iterators are a type of iterator that allows you to iterate over data that may not be available all at once. By implementing asynchronous iterator in your Sequence you are able to better handle data coming from asynchronous sources, such as external network requests or as data sent to Sequence's input
.
The following example demonstrates how to easily implement asynchronous iterator for reading data sent to Sequence's input
. The main Sequence function reads data as the come to its input
- item by item. For each data item received on input, a function mocking an asynchronous action of saving item in the database is called. This example illustrates how simply and effectively use iterator and generator pattern together to process data asynchronously.
- JavaScript
- TypeScript
- Python
async function saveEntryToDB(data){
return "saved";
}
module.exports = async function* (input) {
for await (const chunk of input) {
yield await saveEntryToDB(chunk);
}
}
import { TransformApp } from "@scramjet/types";
async function saveEntryToDB(data){
return "saved";
}
const mod: TransformApp = async function* (_input) {
for await (const chunk of _input) {
yield await saveEntryToDB(chunk);
}
}
export default mod;
import asyncio
async def save_entry_to_db(data):
return "saved"
async def run(context, input):
async for chunk in input:
yield await save_entry_to_db(chunk)
To create a package.json file with the values that you supply, use the command-line
npm init -
. It creates a new package.json file with elementary properties containing prompts about the project's name, version, etc. More information on the package.json file shown in the illustration below.
- JavaScript
- TypeScript
- Python
{
"name": "@scramjet/js-consumer",
"version": "1.0.0",
"description": "Scramjet Cloud Platform Sequence Javascript Template",
"main": "index.js",
"scripts": {
"predeploy": "mkdir -p dist/ && cp index.js package.json dist/ && (cd dist && npm i --omit=dev)"
},
"engines": {
"node": ">=16"
},
"repository": {
"type": "git",
"url": "git+https://github.com/scramjetorg/create-sequence.git"
},
"bugs": {
"url": "https://github.com/scramjetorg/create-sequence/issues"
},
"homepage": "https://github.com/scramjetorg/create-sequence#readme"
}
{
"name": "@scramjet/ts-consumer",
"version": "1.0.0",
"description": "Scramjet Cloud Platform Sequence Typescript Template",
"main": "index.js",
"scripts": {
"predeploy" : "tsc -p tsconfig.json && cp package.json dist/ && (cd dist && npm i --omit=dev)"
},
"engines": {
"node": ">=16"
},
"repository": {
"type": "git",
"url": "git+https://github.com/scramjetorg/create-sequence.git"
},
"bugs": {
"url": "https://github.com/scramjetorg/create-sequence/issues"
},
"homepage": "https://github.com/scramjetorg/create-sequence#readme",
"devDependencies": {
"@scramjet/types": "^0.31.2",
"typescript": "^4.9.4",
"@types/node": "15.12.5",
"scramjet": "^4.36.9"
}
}
{
"name": "@scramjet/py-consumer",
"version": "1.0.0",
"description": "Scramjet Cloud Platform Sequence Python Template",
"main": "main.py",
"scripts": {
"predeploy": "mkdir -p dist/__pypackages__/ && cp *.py package.json dist/ && pip3 install -t dist/__pypackages__/ -r requirements.txt"
},
"engines": {
"python3": ">=3.8"
},
"repository": {
"type": "git",
"url": "git+https://github.com/scramjetorg/create-sequence.git"
},
"bugs": {
"url": "https://github.com/scramjetorg/create-sequence/issues"
},
"homepage": "https://github.com/scramjetorg/create-sequence#readme"
}
Related resources
- Visit Scramjet's Sample GitHub repository for more examples of Sequences.