Skip to content

Instantly share code, notes, and snippets.

@yf-hk
Last active August 29, 2015 14:01
Show Gist options
  • Save yf-hk/c0b3e2c262ad3a17ec3b to your computer and use it in GitHub Desktop.
Save yf-hk/c0b3e2c262ad3a17ec3b to your computer and use it in GitHub Desktop.
var Promise = require('bluebird');
var request = Promise.promisifyAll(require('request'));
ENABLE_FIDDLER_PROXY = false;
var proxy = ENABLE_FIDDLER_PROXY ? {proxy: 'http://127.0.0.1:8888'} : {};
console.log('before all requests');
// Google and v2ex are parallel requests
// It loads baidu only after google is finished
// When they are all finished, it goes to the final spread function
Promise.all([
request.getAsync('http://www.google.com.hk', proxy)
.then(function(google) {
console.log('google done, loading baidu')
return request.getAsync('http://www.baidu.com', proxy)
}),
request.getAsync('http://v2ex.com', proxy)])
.spread(function(baidu, v2ex) {
console.log('all done');
});
console.log('google & v2ex started');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment