parallelPromiseImplementations

In this example below you will see how to do a parallelPromiseImplementations with some HTML / CSS and Javascript

Thumbnail
This awesome code was written by 15chrjef, you can see more from this user in the personal repository.
You can find the original code on Codepen.io
Copyright 15chrjef ©
  • HTML
  • JavaScript
<!DOCTYPE html>
<html lang="en" >

<head>
  <meta charset="UTF-8">
  <title>parallelPromiseImplementations</title>
  
  
  
  
  
</head>

<body>

  
  
  

    <script  src="js/index.js"></script>




</body>

</html>

/*Downloaded from https://www.codeseek.co/15chrjef/parallelpromiseimplementations-mRxRMx */
function parallel(tasks, cb) {
	var results = [];
	var counter = 0;
	tasks.forEach((task, i) => {
	  console.log(i,results )
		tasks[i]((err, data) => {
		  console.log(i, err, data)
			if(err) {
				return cb(err, null)
			}
			counter ++;
			results[i] = data
			if( counter === tasks.length ) {
			  console.log('results')
				return cb(null, results)
			}
	  })
	})
}

var myTasks = [
  (cb) => setTimeout(() => {
    console.log(60)
    cb(null,60)
  }, 600),
  (cb) => setTimeout(() => { 
    console.log(50)
    cb(null,50)
  }, 500), 
  (cb) => setTimeout(() => { 
    console.log(40)
    cb(null,40)
  }, 400), 
  (cb) => setTimeout(() => { 
    console.log(30)
    cb(null, 30)
  }, 300)
]

var fn = (err, data) => {
  if(err) {
    console.log('err',err)
  }else {
    console.log('data', data)
  }
}

parallel(myTasks, fn)

Comments