Skip to main content

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.

async function saveEntryToDB(data){
return "saved";
}

module.exports = async function* (input) {
for await (const chunk of input) {
yield await saveEntryToDB(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.

{
"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"
}
  • GitHub logo Visit Scramjet's Sample GitHub repository for more examples of Sequences.

Was it helpful?

Didn't find information needed?

Join our Scramjet Community on Discord, where you can get help from our engineers directly.