Skip to content

Instantly share code, notes, and snippets.

@hiroshi-maybe
Created June 20, 2019 04:05
Show Gist options
  • Save hiroshi-maybe/d2f61b98554296617989ac72164c0bcb to your computer and use it in GitHub Desktop.
Save hiroshi-maybe/d2f61b98554296617989ac72164c0bcb to your computer and use it in GitHub Desktop.
const _ = require('lodash');
const task = (n) => {
return new Promise((resolve, reject)=> {
setTimeout(() => {
console.log("t"+n+" done");
resolve(n);
}, 1000*n);
});
};
const check = (message) => {
return (res) => {
console.log(res, 'received');
return new Promise((resolve, reject) => {
if(message) console.log(message);
resolve();
});
};
};
const seq = () => {
return task(1)
.then(() => task(2))
.then(() => task(3))
.then(check('completed'));
};
const par = () => {
return Promise.all([task(1),task(2),task(3)])
.then(check())
.then((xs) => task(4))
.then(check());
};
seq().then(par);
//par();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment