Skip to content

Instantly share code, notes, and snippets.

View jguix's full-sized avatar

Juan G. Jordán jguix

View GitHub Profile
@jguix
jguix / capture-photo.js
Last active March 5, 2018 15:00
Capture photo in an Ionic app and move the photo to permanent storage
// From: http://www.joshmorony.com/store-camera-photos-permanently-using-phonegap-ionic-ngcordova/
$cordovaCamera.getPicture(options).then(function(imagePath){
//Grab the file name of the photo in the temporary directory
var currentName = imagePath.replace(/^.*[\\\/]/, '');
//Create a new name for the photo
var d = new Date(),
n = d.getTime(),
newFileName = n + ".jpg";
@jguix
jguix / recursive-promise.ts
Last active December 10, 2022 16:22
How to create a recursive promise chain. Source http://jsbin.com/qotabib/edit?js,console
// A recursive function returning Promise<number>
function recurseToZero(n) {
console.log('B. Entering recursive function for [' + n + '].');
// Once we hit zero, bail out of the recursion. The key to recursion is that
// it stops at some point, and the callstack can be "rolled" back up.
if (n === 0) {
// We could just return 0 but we do return Promise.resolve to have a consistent return type
return Promise.resolve(0);
}
// Start a NEW PROMISE CHAIN that will become the continuation of the parent
@jguix
jguix / recursive-observable.ts
Last active March 5, 2018 15:38
How to create a recursive observable pipe. Source: http://jsbin.com/finisab/edit?js,console
// A recursive function returning Observable<number>
function recurseToZero(n) {
console.log('B. Entering recursive function for [' + n + '].');
// Once we hit zero, bail out of the recursion. The key to recursion is that
// it stops at some point, and the callstack can be "rolled" back up.
if (n === 0) {
return Rx.Observable.of(0);
}
// Start a NEW PROMISE CHAIN that will become the continuation of the parent
// promise chain. This new promise chain now becomes a completely tangential