console.log(`value is ${current} at index ${index}`);
// call next once done,
// foreach only takes an argument,
// if error is passed, the loop will halt immediately
next(null);
}
).then(()=>{
// foreach does not return any value on completion
console.log('done');
}).catch((error)=>{
if(error){
console.log(error);
}
});
Reduce
const{reduce}=require('async-array-loop');
constsomeNums=[1,2,3,4];
reduce(
someNums,
// the processing function
(prevValue,nextValue,next)=>{
let sum = prevValue + nextValue;
// call next once done,
// first argument is error,
// second argument is the value to return
next(null, sum);
},
// called when complete
(error,result)=>{
if(error){
console.log(error);
return;
}
console.log(result);// => 10
}
);
// it is also possible to pass an initial value
let initialValue =50;
reduce(
someNums,
// the processing function
(prevValue,nextValue,next)=>{
let sum = prevValue + nextValue;
// call next once done,
// first argument is error,
// second argument is the value to return
next(null, sum);
},
// called when complete
(error,result)=>{
if(error){
console.log(error);
return;
}
console.log(result);// => 60
},
initialValue
);
Reduce(with promise)
const{reducePromise}=require('async-array-loop');
constsomeNums=[1,2,3,4];
reducePromise(
someNums,
// the processing function
(prevValue,nextValue,next)=>{
// double each number
let sum = prevValue + nextValue;
// call next once done,
// first argument is error,
// second argument is the value to return
next(null, sum);
}
).then((result)=>{
console.log(result);// => 10
}).catch((error)=>{
if(error){
console.log(error);
}
});
// it is also possible to pass an initial value
let initialValue =20;
reducePromise(
someNums,
// the processing function
(prevValue,nextValue,next)=>{
// double each number
let sum = prevValue + nextValue;
// call next once done,
// first argument is error,
// second argument is the value to return
next(null, sum);
},
initialValue // we pass an initial value
).then((result)=>{
console.log(result);// => 30
}).catch((error)=>{
if(error){
console.log(error);
}
});
Map
const{map}=require('async-array-loop');
constsomeNums=[1,2,3,4];
map(
someNums,
// the processing function
(current,index,array,next)=>{
let doubleOfCurrent = current *2;
// call next once done,
// first argument is error,
// second argument is the value to return
next(null, doubleOfCurrent);
},
// called when complete
(error,result)=>{
if(error){
console.log(error);
return;
}
console.log(result);// => [2,4,6,8]
}
);
Map(with promise)
const{mapPromise}=require('async-array-loop');
constsomeNums=[1,2,3,4];
mapPromise(
someNums,
// the processing function
(current,index,array,next)=>{
// double each number
let doubleOfCurrent = current *2;
// call next once done,
// first argument is error,
// second argument is the value to return
next(null, doubleOfCurrent);
}
).then((result)=>{
console.log(result);// => [2,4,6,8]
}).catch((error)=>{
if(error){
console.log(error);
}
});
Tests
To test first run npm install and finally npm run test
Contributing
This library(excluding tests) is written following the XO coding style.
All kinds of improvements/suggestions/PRs are welcomed.
Please write tests for any changes you want to make and submit a PR. :)