Actor picture

Example Counter

apify/example-counter

This actor simply counts up from one. In each run it prints one number. Its state (counter position) is stored in a named key-value store. The name of the store is example-counter and you can find in Apify app under Storages.

No credit card required

Author's avatarApify Technologies
  • Modified
  • Users10
  • Runs168

Based on the apify/actor-node-basic:v0.21.10 Docker image (see docs).

const Apify = require('apify');

Apify.main(async () => {
    // Get store with name 'example-counter'.
    const store = await Apify.openKeyValueStore('example-counter');

    // Get counter value from store
    const record = await store.getValue('counter');
    
    // If there is no such record then start from zero.
    let counter = record || 0;

    // Increase counter, print and set as output.
    counter ++;
    console.log(`Counter: ${counter}`);
    Apify.setValue('OUTPUT', counter);
    
    // Update the value in store
    await store.setValue('counter', counter);
});