Skip to content
This repository has been archived by the owner on Jan 19, 2024. It is now read-only.

Commit

Permalink
Append "catch" to handle fails - fix #103
Browse files Browse the repository at this point in the history
  • Loading branch information
jbdemonte committed Dec 3, 2016
1 parent d6479ab commit e1b2aa0
Show file tree
Hide file tree
Showing 3 changed files with 78 additions and 5 deletions.
20 changes: 16 additions & 4 deletions src/gmap3.js
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@
if (status === gm.GeocoderStatus.OK) {
dfd.resolve(results[0].geometry.location);
} else {
dfd.reject();
dfd.reject(status);
}
});
return dfd;
Expand Down Expand Up @@ -263,13 +263,16 @@
if (address) {
delete options.address;
return geocode(address).then(function (latLng) {
options[key] = latLng;
});
options[key] = latLng;
});
}
options[key] = toLatLng(options[key]);
})
.then(function () {
dfd.resolve(fn(options));
})
.fail(function (reason) {
dfd.reject(reason);
});
return dfd;
}
Expand Down Expand Up @@ -892,7 +895,7 @@
/**
* Decorator to chain promise result onto the main promise chain
* @param {Function} fn
* @returns {Deferred}
* @returns {Function}
*/
function chainToPromise(fn) {
return function () {
Expand Down Expand Up @@ -1085,6 +1088,15 @@
}
};

self.catch = function (fn) {
if (isFunction(fn)) {
promise = promise
.then(null, function (reason) {
return when(fn.call(context(), reason));
});
}
};

foreachStr('on once', function (name, once) {
self[name] = function () {
var events = arguments[0];
Expand Down
50 changes: 50 additions & 0 deletions tests/tests/chaining.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,15 @@
describe('chaining', function () {

function rejected(reason) {
var dfd = jQuery.Deferred();
dfd.reject(reason);
return dfd;
}
function resolved(reason) {
var dfd = jQuery.Deferred();
dfd.resolve(reason);
return dfd;
}

beforeEach(function (done) {
var self = this;
Expand Down Expand Up @@ -100,4 +111,43 @@ describe('chaining', function () {
});
});

it('should chain catch', function (done) {
var results = [];
this.handler
.marker(function (map) {
expect(map).to.be.an.instanceof(google.maps.Map);
return rejected(1);
})
.then(function () {
done(new Error('catch expected'));
})
.catch(function (value) {
results.push(value);
return resolved(2);
})
.then(function (value) {
results.push(value);
return rejected(3);
})
.then(function () {
done(new Error('catch expected'));
})
.catch(function (value) {
results.push(value);
return rejected(4);
})
.then(function () {
done(new Error('catch expected'));
})
.catch(function (value) {
results.push(value);
return 5;
})
.then(function (value) {
results.push(value);
expect(results).to.eql([1,2,3,4,5]);
done();
});
});

});
13 changes: 12 additions & 1 deletion tests/tests/latlng.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,18 @@ describe('latlng', function () {
});
});

it('would handle latlng fail', function (done) {
this.handler
.latlng({address: '>>>>>'})
.then(function () {
done(new Error('error expected'));
})
.catch(function (err) {
expect(err).to.be.equal('ZERO_RESULTS');
done();
});
});

it('would convert the position as array', function (done) {
this.handler
.latlng({latlng: [10,20]})
Expand All @@ -39,5 +51,4 @@ describe('latlng', function () {
done();
});
});

});

0 comments on commit e1b2aa0

Please sign in to comment.