1.1.0 • Published 7 years ago
forawait v1.1.0
Methods:
Note, that every function returns Promise pending end of all operations.
forAwait
Structure:
forAwait(array, func, this_arg = null);
func.call(this_arg, next, value, iteration, array)
Example:
forAwait([1, 2, 3], (next, value, iteration, array) => {
console.log(value * 2);
next();
});
// 2
// 4
// 6
mapAwait
Structure:
forAwait(array, func, this_arg = null);
func.call(this_arg, next, value, iteration, array)
You can pass changed item value as next
argument.
Example:
mapAwait([1, 2, 3], (next, value, iteration, array) => {
next(value * 3);
}).then(changed_array => {
console.log(changed_array);
});
// [3, 6, 9]
reduceAwait
Structure:
reduceAwait(arr, func, initial_value = arr[0]);
func.call(null, next, accumulator, value, iteration, array)
Example:
reduceAwait([1, 2, 3], (next, accumulator, value) => {
next(accumulator+value);
})
.then(result => console.log(result));
// 6
filterAwait
Structure:
filterAwait(array, func, this_arg = null);
func.call(this_arg, next, value, iteration, array)
If you want to pass item, call next(true)
.
Example:
filterAwait([0, 2, 4, 6, 8, 10], (next, value) => {
next(value > 5);
})
.then(filtered => console.log(filtered));
// [6, 8, 10]
And... that's all!
Using this_arg
example:
mapAwait([1, 2, 3], function(next, value){
const { multiplier } = this; //destructuring assignment
next(value * multiplier);
}, {
multiplier: 10
}).then(changed_array => {
console.log(changed_array);
});
You can't use ES6 arrow functions, because arrow function does not create its own this
, the this
value of the enclosing execution context is used.