Generators
The simplest way to expose data from your Sequence is by following a generator pattern. Generators provide a powerful and flexible approach to writing asynchronous code.
The generator will create new data item on each function call, exposing this data item on the Sequence output stream. In this example you see the yield keyword is used to expose items obtained from the mock API fetching function.
A useful feature of JavaScript generators is that those can yield* - this means that an iterator or an array can be passed and therefore multiple chunks can be send to the output stream. This is advisable for efficiency reasons.
Generators have one additional benefit: they will not produce more data if data is not read. If you start a generator in your Sequence but not read from it, the program will run a couple initial iterations to fill in the buffers, but eventually it will stop at yield and wait until you read the data. You can read the data generated by the Sequence for example using the Scramjet Instance API.
- JavaScript
- TypeScript
- Python
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
async function getPageFromApi(){
return "Your result";
}
module.exports = async function* () {
while(true){
yield await getPageFromApi();
await sleep(1000)
}
}
import { ReadableApp } from "@scramjet/types";
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
function getPageFromApi(){
return "Your result";
};
const mod: ReadableApp = async function* () {
while(true){
yield await getPageFromApi();
await sleep(1000)
}
}
export default mod;
import asyncio
def get_page_from_api():
return "Your result \n"
async def run(context, input):
while True:
for result in get_page_from_api():
yield result
await asyncio.sleep(5)
The simplest way to run a generator function in a Sequence is to save the below JSON under the name package.json for it to be packed with the main file as a tar.gz.
Or 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-task",
"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-task",
"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-task",
"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
-
Check out Scramjet's Command lines Interface (CLI) section.
-
Visit Scramjet's Sample GitHub repository for more examples of Sequences.