-
-
Save boo1ean/c4a2775b29e02c3556fd to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var Q = require('q'); | |
var request = require('request'); | |
var url = 'https://api.github.com/users/boo1ean'; | |
var userAgent = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2062.94 Safari/537.36'; | |
var getInfo = function() { | |
var deferred = Q.defer(); | |
var opt = { | |
url: url, | |
headers: { | |
'User-Agent': userAgent | |
} | |
}; | |
request(opt, function(err, res, body) { | |
if (err || res.statusCode != 200) { | |
deferred.reject('hahaha'); | |
} else { | |
deferred.resolve(body); | |
} | |
}); | |
return deferred.promise; | |
}; | |
var getError = function(x) { | |
var deferred = Q.defer(); | |
setTimeout(function() { | |
deferred.resolve(x + ' seconds!'); | |
}, 2000); | |
return deferred.promise; | |
}; | |
Q.all([ | |
getError(1), | |
getError(2), | |
getError(3), | |
getError(4) | |
]).then(function(results) { | |
console.log(results); | |
}); | |
getInfo() | |
.then(function(body) { | |
console.log(body); | |
return getError(); | |
}).then(function(number) { | |
console.log(number); | |
return 2; | |
}).then(function(number) { | |
console.log(number); | |
}).catch(function(reason) { | |
console.log('ERROR', reason); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment