Last active
August 29, 2015 14:08
-
-
Save bbrewer97202/08553b2509c2155d63bf to your computer and use it in GitHub Desktop.
Chained promises with q.js
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
<!doctype html> | |
<html> | |
<head> | |
<title></title> | |
<script type="text/javascript" src="http://cdnjs.cloudflare.com/ajax/libs/q.js/1.0.1/q.min.js"></script> | |
</head> | |
<body> | |
<script type="text/javascript"> | |
//watch browser console for notification of what is happening | |
//each method resolves or rejects a single argument that is passed through the chain until the end | |
//errors in any point of the chain are handled by the catch | |
getGeoLocation() | |
.then(setLocationData) | |
.then(makeDataCall) | |
.then(plotMarkers) | |
.then(function(data) { | |
console.log("the end", data); | |
}) | |
.catch(function(response) { | |
if (response.error) { | |
console.log("error!" + response.error); | |
} | |
}); | |
function getGeoLocation() { | |
var deferred = Q.defer(); | |
console.log("getGeoLocation() called"); | |
setTimeout(function() { | |
if (true) { | |
console.log("getGeoLocation() responds success"); | |
deferred.resolve({ | |
lat: "1", | |
lon: "1" | |
}) | |
} else { | |
deferred.reject({ | |
error: "getGeoLocation() error" | |
}); | |
} | |
}, 1000); | |
return deferred.promise; | |
} | |
function setLocationData(latLon) { | |
var deferred = Q.defer(); | |
console.log("setLocationData() called: lat=" + latLon.lat + ", lon=" + latLon.lon); | |
setTimeout(function() { | |
if (typeof latLon === 'object') { | |
console.log("setLocationData() responds success"); | |
deferred.resolve(latLon); | |
} else { | |
console.log("setLocationData() responds error"); | |
deferred.reject({ | |
error: "setLocationData() error" | |
}); | |
} | |
}, 1000); | |
return deferred.promise; | |
} | |
function makeDataCall(latLon) { | |
var deferred = Q.defer(); | |
console.log("makeDataCall() called: lat=" + latLon.lat + ", lon=" + latLon.lon); | |
setTimeout(function() { | |
if (typeof latLon === 'object') { | |
console.log("makeDataCall() responds success"); | |
deferred.resolve({ | |
foo: '1', | |
bar: '2' | |
}); | |
} else { | |
console.log("makeDataCall() responds error"); | |
deferred.reject({ | |
error: "makeDataCall() error" | |
}); | |
} | |
}, 1000); | |
return deferred.promise; | |
} | |
function plotMarkers(data) { | |
var deferred = Q.defer(); | |
console.log("plotMarkers() called: data=", data); | |
setTimeout(function() { | |
if (typeof data === 'object') { | |
console.log("plotMarkers() responds success"); | |
deferred.resolve(data); | |
} else { | |
console.log("plotMarkers() responds error"); | |
deferred.reject({ | |
error: "plotMarkers() error" | |
}); | |
} | |
}, 1000); | |
return deferred.promise; | |
} | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment