1.0.2 • Published 9 months ago

syncforeachloop v1.0.2

Weekly downloads
-
License
MIT
Repository
-
Last release
9 months ago

syncForEach is a synchronous version of the forEach loop.

In the example below, each of the cities is printed on the screen.

Install

To install the package via npm, run the following code in the terminal:

$ npm i syncforeachloop

Usage

For JavaScript:

const {} = require('syncforeachloop');

For TypeScript:

import 'syncforeachloop'

Examples

var cities = ["Paris","İstanbul","Berlin"]; //Defined an array of cities.

//Each variable will be assigned to a single city variable.
cities.syncForEach(function (city,next_city,index,length) {
    console.log(index); // Index will be written to the console.
    console.log(length); //Array length (array element count) will be written to the console.
    console.log(city); //Each city will be written on the console.
    next_city(); //This function will skip to the next element.
});

In the example below, shows the use of callbacks.

var cities = ["Paris","İstanbul","Berlin"]; // Defined an array of cities.

// Each variable will be assigned to a single city variable.
cities.syncForEach(function (city,next_city,index,length) {
    console.log(index); // Index will be written to the console.
    console.log(length); // Array length (array element count) will be written to the console.
    console.log(city); // Each city will be written on the console.
    next_city(); // This function will skip to the next element.
},() => { // Calling a callback function after elements in all arrays are complete.
    console.log('Loop Ended');
});

In the example below, shows the use of callbacks from outside.

function callback() {
    console.log('hello');
}

var cities = ["Paris","İstanbul","Berlin"]; // Defined an array of cities.

cities.syncForEach(function (city,next_city,index,length) {
    console.log(index);
    console.log(length);
    console.log(city); 
    next_city(); 
},callback); // It is possible to call a function from outside.