Last active
December 16, 2015 02:48
-
-
Save davisford/5364793 to your computer and use it in GitHub Desktop.
AngularJS promise problem on embedded device w/WebKit. The problem I am seeing is that a call to `deferred.resolve( )` does not end up reaching the promise's `then(fn, fn)` success callback function. Sometimes it does, but sometimes it disappears into the ether, and I can't figure out why. There is a global Angular exception handler which is not…
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
// error handling is all trapped with window.onerror; I also override Angular $exceptionHandler which both | |
// post back to the remote logger service. I have also inserted try/catch blocks in the code below which | |
// never gets into the catch block; net summary: I am not seeing any erorr thrown which would be the root | |
// cause of this issue. | |
(function () { | |
var app = angular.module('app'); | |
/** | |
* HTML looks simple like this: | |
* | |
* <div ng-controller="FooCtrl"> | |
* <p>promise state: {{state}}</p> | |
* <p>data: {{data | json}}</p> | |
* </div> | |
*/ | |
app.controller('FooCtrl', ['$scope', 'myservice', 'logger', function ($scope, myservice, logger) { | |
$scope.state = 'deferred'; | |
var promise = myservice.get(); | |
promise.then( | |
// SOMETIMES: this bell is never rung, despite the remote logger indicating | |
// that the call to foo.callAsynFn gets into the it's success handler and | |
// subsequently calls deferred.resolve(data). According to $q source, on | |
// nextTick() this function should be called, but it does not always happen | |
function success(data) { | |
logger.log('promise resolved success'); | |
$scope.data = data; | |
$scope.state = 'resolved'; | |
}, | |
// BTW: this never happens, so it isn't a case of an error condition | |
function error(err) { | |
logger.log('promise failed to resolve'); | |
$scope.state = 'failed'; | |
} | |
); | |
}]); | |
// remote logger service b/c I do not have console.log available | |
app.factory('logger', function () { | |
return { | |
log: function (msg) { | |
// remote log message to a server | |
} | |
} | |
}); | |
// this service wraps another async dependency service | |
// using promises here -- but that seems to be part of the issue... | |
app.factory('myserivce', ['$q', '$rootScope', 'logger', function($q, $rootScope, logger) { | |
return { | |
get: function() { | |
var deferred = $q.defer(); | |
$rootScope.$apply(function () { | |
// assume foo is a global service i'm calling async which takes a success / failure callback | |
foo.callAsyncFn( | |
function success(data) { | |
// the remote logger indicates that this always happens | |
logger.log('callAsyncFn success'); | |
// SOMETIMES: this never reaches the controllers .then() success function | |
promise.resolve(data); | |
}, | |
function error(err) { | |
logger.log('callAsyncFn error'); | |
return promise.reject(err); | |
} | |
); // end foo.callAsyncFn | |
}); // end $apply | |
} | |
}; | |
}]); | |
angular.bootstrap(document, ['app']); | |
}()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Did you ever discover a fix for this? I'm running into the same problem, my promises die silently in iOS/phonegap. Any help would be MUCH appreciated.