Skip to content

Instantly share code, notes, and snippets.

@artemave
Last active February 12, 2016 12:18
Show Gist options
  • Select an option

  • Save artemave/6eec0e0ecb8a149e2360 to your computer and use it in GitHub Desktop.

Select an option

Save artemave/6eec0e0ecb8a149e2360 to your computer and use it in GitHub Desktop.
Batches of concurrent jobs with js promises
var http = require('httpism');
var _ = require('underscore');
var p1 = () => http.get('http://google.com').then(() => console.log('p1'))
var p2 = () => http.get('http://google.com').then(() => console.log('p2'))
var p3 = () => http.get('http://google.com').then(() => console.log('p3'))
var p4 = () => http.get('http://google.com').then(() => console.log('p4'))
var a1 = () => Promise.all([p1(), p2()]).then(() => console.log('a1'))
var a2 = () => Promise.all([p3(), p4()]).then(() => console.log('a2'))
_.reduce([a1, a2], (seq, p) => seq.then(p), Promise.resolve()).then(() => console.log("THE END"))
// `reduce` is the same as:
// Promise.resolve().then(a1).then(a2).then(() => console.log("THE END"))
@artemave
Copy link
Author

screen shot 2016-02-12 at 12 13 08

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment