Skip to content

Instantly share code, notes, and snippets.

@mapsam
Forked from ranchodeluxe/gist:6707360
Created September 28, 2013 06:07
Show Gist options
  • Save mapsam/6738978 to your computer and use it in GitHub Desktop.
Save mapsam/6738978 to your computer and use it in GitHub Desktop.
#
# Sam, for some weird reason the promise.done()
# function wasn't able to work for me now,
# I'll figure it out later...for now were are using
# .then for concurrent requests...let's chat about this
#
#
# this is a standalone example
# so just copy to a file and load into your browser
# should run and populate divs
#
<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script type="text/javascript" src="https://rawgithub.com/cugos/cugos.github.com/master/js/Q.js"></script>
</head>
<body>
<script type="text/javascript">
/*
**
**
** Make Requests, Return Promises
**
**
*/
//Q.stopUnhandledRejectionTracking();
var requestP = function( options ) {
var deferred = Q.defer();
var timeout_millisecs = options.timeout_millisecs || 20000; // 20 second timeout
setTimeout(function() {
deferred.reject(new Error("Timeout exceeded. The ArcServer API seems to be slow right now, try it again later"));
}, timeout_millisecs);
var params = options.params || {};
var jqxhr_options = {
url: options.URL || "https://api.github.com/users",
async: options.async || true,
dataType: "json",
data: params,
type : options.verb || 'GET'
};
var jqxhr = $.ajax(jqxhr_options);
var parser_callback = options.parser_callback || function (data, status, xhr) {
/*
**
** If any of these things happen then throw an error
** 1. data is undefined
** 2. presence of data.message means there's an error
** 3. it's an array of length 0
**
*/
if ( typeof data === 'undefined' ||
typeof data.message !== 'undefined' ||
( typeof data === 'object' && data instanceof Array && data.length === 0 ) ) {
deferred.reject( new Error('data response is empty or returned a data.message error')); // stop all promise invocations in the chain
}
deferred.resolve(data);
}; // END parser_callback
jqxhr.success(parser_callback);
jqxhr.error(function (jqXHR, status, error) {
deferred.reject(new Error(error)); // stop all invocations in the chain
});
return deferred.promise;
};
$( document ).ready( function () {
/*
**
** shows both concurrent ( anyting off of get_specific_user ) and synchronized ( .then off those brnaches ) requests after the first one
** to get all related "_url" in the user JSON
**
*/
// main request
var get_specific_user = requestP( { URL : 'https://api.github.com/users/thebigspoon' } );
// go get specific user repos
get_specific_user.then( function( user_json ){
// get the repos for the user
if( user_json.repos_url === 'undefined' ) {
throw new Error('repos_url not in response');
}
// add the user to the output
$('#output #user').append( '<div>' + user_json.name + ", " + user_json.company + '</div>' );
// get a new promise for this request
return requestP( { URL : user_json.repos_url } );
})
.then( function( repos_array ) {
for( var r in repos_array ){
var repo = repos_array[r];
// add the repos to the output
$('#output #repos').append( '<div>' + repo.full_name + '</div>' );
}
})
.catch( function( error ){
if( /repos_url/i.test( error ) ){
console.log( "Handle errors for no repos_url here" );
}
else if ( /data.message/i.test( error ) ){
console.log( "handle bad responses from github API here" );
}
else if ( /TIMEOUT/i.test( error ) ) {
console.log( "Handle TIMEOUTs here" );
}
});
// concurrenlty go get user organizations
get_specific_user.then( function( user_json ){
if( user_json.organizations_url === 'undefined' ) {
throw new Error('organizations_url not in response');
}
// get a new promise for this request
return requestP( { URL : user_json.organizations_url } );
})
.then( function( org_array ) {
for( var r in org_array ){
var org = org_array[r];
// add the org to the output
$('#output #orgs').append( '<div>' + org.login + '</div>' );
}
})
.catch( function( error ){
if( /organizations_url/i.test( error ) ){
console.log( "Handle errors for no organizations_url here" );
}
else if ( /data.message/i.test( error ) ){
console.log( "handle bad responses from github API here" );
}
else if ( /TIMEOUT/i.test( error ) ) {
console.log( "Handle TIMEOUTs here" );
}
});
});
</script>
<div id="output">
<div id="user">
</div>
<hr>
<div id="repos">
</div>
<hr>
<div id="orgs">
</div>
</div>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment