angularjs - How would i create a promise function in this situation with javascript -


im building app using angularjs , using mediafire javascript sdk tasks.

now i'm in situation when upload happens need create folder, function returns 'folderkey'. keep code clean in controller use .then on service functions continue through process of uploading new file.

i have done fair bit if research still left baffled how it. perhaps example in situation me understand needs done.

currently run function service create folder.

mediafireservice.createfolder('testfolder'); 

i have

 mediafireservice.createfolder('testfolder');     .then(function(folderkey){        //upload file folder     }); 

here factory functions.

  function createfolder(foldername){       var options = { foldername: foldername };       login(function(){        app.api('folder/create', options, function(data) {          console.log(data.response.folderkey);          return data.response.folderkey;        });      });    }     function login(callback){       app.login({        email: 'email',        password: 'pass'      }, callback);     }; 

reading around i'm not if javacript has natively or if have use external service or not. kind of lost on @ moment.

any appreciated, thanks.

basically haven't created promise inside createfolder function of service. build custom promise using $q dependency.

return deferred promise method & resolve or reject promise based on server replies.

service

function createfolder(foldername) {   var deferred = $q.defer();   var options = {     foldername: foldername   };   login(function() {     app.api('folder/create', options, function(data) { //success       console.log(data.response.folderkey);       //this line promise resolve       //also send folderkey consumer method.       deferred.resolve(data.response.folderkey);      }, function(data) { //error       //this line promise reject error msg       deferred.reject('error ocurred');     });   });   return deferred.promise; }; 

controller

mediafireservice.createfolder('testfolder').then(function(folderkey){    //upload file folder }, function(error){    //error handling }); 

Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

css - Make div keyboard-scrollable in jQuery Mobile? -

ruby on rails - Seeing duplicate requests handled with Unicorn -