Last active
August 29, 2015 14:20
-
-
Save ionox0/4dfd32fb73d8433522cc to your computer and use it in GitHub Desktop.
ES6 Promises
This file contains hidden or 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
/**** | |
Implemented for gathering Allen Brain Atlas API data | |
[ionox0 - Allen Brain Visualizer](https://github.com/ionox0/Allen-Brain-Atlas-V2) | |
****/ | |
/*jshint esnext: true */ | |
var _ = require('underscore'); | |
var RSVP = require('rsvp'); | |
module.exports = { | |
corsproxy: 'http://localhost:9292/', | |
base: 'api.brain-map.org', | |
path: '/api/v2/data/query.json', | |
getExpressionData: function(geneAcronym){ | |
var thiz = this; | |
return this.requestProbeId(geneAcronym).then(function(data){ | |
return thiz.requestExprVals(data.msg[0].id); // Take the first of the returned probes | |
}, function(error){ | |
console.error(error); | |
}); | |
}, | |
requestProbeId: function(geneAcronym){ | |
console.log("REQUESTPROBEID", geneAcronym); | |
var queryString = "?criteria=model::Probe," + | |
"rma::criteria,[probe_type$eq'DNA']," + | |
"products[abbreviation$eq'HumanMA']," + | |
"gene[acronym$eq'" + geneAcronym + "']," + | |
"rma::options[only$eq'probes.id']"; | |
var url = this.corsproxy + this.base + this.path + queryString; | |
var promise = this.sendXhrReturnPromise(url); | |
return promise; | |
}, | |
requestExprVals: function(probeId){ | |
console.log("REQUESTEXPRVALS", probeId); | |
var queryString = "?criteria=" + | |
"service::human_microarray_expression" + | |
"[probes$eq" + probeId + "]"; | |
// "[donors$eq15496]"; // Specify donor | |
// "[structures$eq9148]"; // Specify structure | |
var url = this.corsproxy + this.base + this.path + queryString; | |
return this.sendXhrReturnPromise(url); | |
}, | |
sendXhrReturnPromise: function(url){ | |
var promise = new Promise(function(resolve, reject){ | |
var request = new XMLHttpRequest(); | |
request.open('GET', url); | |
request.onload = function(){ | |
if (request.status == 200){ | |
resolve(JSON.parse(request.response)); | |
} else { | |
reject(Error(request.statusText)); | |
} | |
}; | |
request.send(); | |
}); | |
return promise; | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment