async-reduce-p

1.0.0 • Public • Published

asyncReduce

Combine Promise and Array.prototype.reduce into asyncReduce

Use

const asyncReduce = require('asyncReduce');

const list = [1,2,3,4,5];

async function sleep(time){
  await new Promise((resolve) => {
    setTimeout(resolve, time);
  });
}

asyncReduce(list, async (pre,cur,index,array)=>{
  const totle = pre + cur;
  console.log(totle);
  await sleep(1000);
  return totle; 
},0)
.then(value => console.log(value))
.catch(err=> console.log(err));

output:

1
(after 1000ms)
3
(after 1000ms)
6
(after 1000ms)
10
(after 1000ms)
15
(after 1000ms)
15

Readme

Keywords

Package Sidebar

Install

npm i async-reduce-p

Weekly Downloads

2

Version

1.0.0

License

MIT

Unpacked Size

3.4 kB

Total Files

5

Last publish

Collaborators

  • rococolate