1.0.2 • Published 5 years ago

sync-foreach v1.0.2

Weekly downloads
61
License
ISC
Repository
github
Last release
5 years ago

Sync Foreach

Simple to use

Sometimes we need to run some async task inside of our loops. Sync-foreach allow us to do this in a easy way

Quick Start

syncforeach(element, function, context)

// Node
const syncforeach = require('sync-foreach')
Browser
<script src="/node_modules/sync-foreach/sync-foreach.js"></script>

Array

syncforeach(['foo', 'bar'], (next, value, index, array) => {
    // Next: Get the next item
    // Value: foo, bar
    // Index: 0, 1
    // Array: ['foo', 'bar']

    // Do some async function, task, etc...
    asyncFunction(() => {
        // And next
        next()

        // For stop the loop pass 'break' or 'done' as parameter
        // next('break')
    })
}).done(() => {
    console.log('completed')
})

Object

syncforeach({foo: 1, bar: 2}, (next, value, key, object) => {
    // Next: Get the next item
    // Value: 1, 2
    // Key: foo, bar
    // Object: {foo: 1, bar: 2}
    
    // Do some async function, task, etc...
    asyncFunction(() => {
        // And next
        next()

        // For stop the loop pass 'break' or 'done' as parameter
        // next('done')
    })
}).done(() => {
    console.log('completed')
})