summaryrefslogtreecommitdiff
path: root/platform/node/index.js
diff options
context:
space:
mode:
Diffstat (limited to 'platform/node/index.js')
-rw-r--r--platform/node/index.js27
1 files changed, 24 insertions, 3 deletions
diff --git a/platform/node/index.js b/platform/node/index.js
index 54ba5c0dc6..5944a0a27d 100644
--- a/platform/node/index.js
+++ b/platform/node/index.js
@@ -4,6 +4,7 @@
var mbgl = require('../../lib/mapbox_gl_native.node');
var constructor = mbgl.Map.prototype.constructor;
+var process = require('process');
var Map = function(options) {
if (!(options instanceof Object)) {
@@ -18,9 +19,29 @@ var Map = function(options) {
return new constructor(Object.assign(options, {
request: function(req) {
- request(req, function() {
- req.respond.apply(req, arguments);
- });
+ // Protect against `request` implementations that call the callback synchronously,
+ // call it multiple times, or throw exceptions.
+ // http://blog.izs.me/post/59142742143/designing-apis-for-asynchrony
+
+ var responded = false;
+ var callback = function() {
+ var args = arguments;
+ if (!responded) {
+ responded = true;
+ process.nextTick(function() {
+ req.respond.apply(req, args);
+ });
+ } else {
+ console.warn('request function responded multiple times; it should call the callback only once');
+ }
+ };
+
+ try {
+ request(req, callback);
+ } catch (e) {
+ console.warn('request function threw an exception; it should call the callback with an error instead');
+ callback(e);
+ }
}
}));
};