Skip to content

Instantly share code, notes, and snippets.

View EduceHealth's full-sized avatar

Educe Health Ltd. EduceHealth

View GitHub Profile
@EduceHealth
EduceHealth / $q.all.js
Last active August 29, 2015 14:02
$q promises
var q1 = $scope.q1 = $q.defer()
var q2 = $scope.q2 = $q.defer()
var p1 = $scope.q1.promise
var p2 = $scope.q2.promise
$q.all([
p1.then(function(value){console.log('p1 resolved ', value);
return value;}),
p2.then(function(value){console.log('p2 resolved ', value);
return value;})
@EduceHealth
EduceHealth / p.js
Last active August 29, 2015 14:02 — forked from robwormald/p.js
fluent p
//opt 1
SomeService.getStuff().then(function(stuff){
return SomeService.manipulateStuff(stuff);
}).then(function(manipulatedStuff){
//do stuff...
});
//opt 2
SomeService.getStuff().then(SomeService.manipulateStuff).then(function(manipulatedStuff){
//do stuff...
@EduceHealth
EduceHealth / Array
Created June 7, 2014 02:21
Arrays vs Objects.
this.addToResponses = function(responses, interventionId, questiongroupId, questiongroupInstanceId, questionId, response) {
if (!responses.questiongroups) {
responses.interventionId = interventionId
responses.questiongroups = new Array()
}
if(!responses.questiongroups[????????]) {
responses.questiongroups.push({Id: questiongroupId, questiongroupInstances:{}})
}
if (!responses.questiongroups[????????].questiongroupInstances[questiongroupInstanceId]){
responses.questiongroups[????????].questiongroupInstances.push({Id: questiongroupInstanceId, questions:{}})
@EduceHealth
EduceHealth / map vs for
Created June 7, 2014 00:27
What to do instead of [for (var in collection)]
getSomeThings().then(function(things){
return things.map(function(aThing){
return OtherThing.find(aThing.id);
});
});
//Replaces
var thingIx, newThing, otherThings
getSomeThings()
.then(function(collection){
@EduceHealth
EduceHealth / parallel promises
Created June 7, 2014 00:22
Parallel promises in an array
var aBunchOfThings = [];
for(thing in things){
aBunchOfThings.push(OtherThing.find());
}
Q.all(aBunchOfThings).then(function(otherthings){
})