const AsyncArray = require("async-array-class")
// Support function
function sleep(milliseconds) {
return new Promise((resolve) => setTimeout(resolve,milliseconds))
}
console.log('Creating the array')
const baseArray = new AsyncArray(1,2,3)
console.log(baseArray)
console.log((new Date()).toString(), 'Running an async mapping')
const resultArray = await baseArray.mapAsync(( value ) => {
// Wait 1000ms and then return the value multiplied by 2
return sleep(1000).then(()=> {
return value*2
})
})
// After the array is mapped
console.log((new Date()).toString(),resultArray)
console.log((new Date()).toString(), 'Running an async reduce')
const result = await baseArray.reduceAsync(( total, value ) => {
// Wait 500ms and then return the value multiplied by 2
return sleep(500).then(()=> {
return total+value
})
},0)
// After the array is reduced
console.log((new Date()).toString(),result)
Created from: https://tonicdev.com/npm/async-array-class