Promise to create a custom crash is not a function of Angularjs error?

How am I trying to use a promise to create html5 directory functions in angularjs. but it shows an error because the promise is not defined.

Angular file: -

$scope.createDirectory = function(dirName,dirLocation){ fileManager.createDirectory(dirName,dirLocation) .then(function(data){ console.log(data, "dir created"); }).fail(function(err){ console.log(err,"dir err while creating"); }); }; 

JS File: -

 var fileManager = { createDirectory: function(directoryName,dirLocation){ var makePromise = new Promise(function(resolve, reject){ dirLocation.getDirectory(directoryName, {create: true, exclusive: false}, function(data){ resolve(data); }, function(error){ reject(error); }); }); return makePromise; } } TypeError: fileManager.createDirectory(...).then(...).fail is not a function at h.$scope.createDirectory (app.js:60) at angular.min.js:196 at f (angular.min.js:224) at h.$eval (angular.min.js:123) at h.$apply (angular.min.js:123) at HTMLButtonElement.<anonymous> (angular.min.js:224) at HTMLButtonElement.c (angular.min.js:32) 

how to create a promise and bind it to the angular function. I used catch instead of not working. how to create your own error callback for all crash functions like this

+1
source share
1 answer

There is a typo in the promise , it should be new Promise . Although I would suggest you use $q instead of promise .

The advantage of using $q instead of the promise object is that the code will be an angular context, and you do not have to worry about starting the digest cycle manually. Where, as if you were using promise , you need to start the digest cycle manually (since promise will be a built-in asynchronous JS function, considered the 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 is not available in the $q object, you need to change the code of the fileManager.createDirectory function below.

 $scope.createDirectory = function(dirName,dirLocation){ fileManager.createDirectory(dirName,dirLocation) .then(function(data){ //success callback console.log(data, "dir created"); }, function(err){ //error callback console.log(err,"dir err while creating"); }); }; 
+4
source

Source: https://habr.com/ru/post/1013480/


All Articles