Promise Create Custom Fail Is Not A Function Angularjs Error?
As i am trying to use promise to create html5 directory functions in angularjs. but it shows error as promise not defined. Angular file:- $scope.createDirectory = function(dirName,
Solution 1:
There is typo in promise
, It should be new Promise
. Though I would suggest you to use $q
instead of Promise
.
The advantage of using $q
instead of Promise
object is, you code will be angular context & you don't need to care to run digest cycle manually. Where as if you use Promise
then you need to run digest cycle manually(as Promise
would be native asynchronous JS function, considered to be outside world of angular).
createDirectory: function(directoryName, dirLocation) {
var makePromise = $q(function(resolve, reject) {
dirLocation.getDirectory(directoryName, {
create: true,
exclusive: false
}, function(data) {
resolve(data);
}, function(error) {
reject(error);
});
});
return makePromise;
};
Update
.fail
function isn't available on $q
object, you need to change your fileManager.createDirectory
function code call to below.
$scope.createDirectory = function(dirName,dirLocation){
fileManager.createDirectory(dirName,dirLocation)
.then(function(data){ //success callbackconsole.log(data, "dir created");
}, function(err){ //error callbackconsole.log(err,"dir err while creating");
});
};
Post a Comment for "Promise Create Custom Fail Is Not A Function Angularjs Error?"