Solved

how can I set a variable inside the promise that will be accessible outside of the promise?

Posted on 2016-11-15
16
37 Views
Last Modified: 2016-11-16
in the case of this code for example:
      var d = false;
      

      // // Delete the file
      LimageRef.delete().then(function() {
        //console.log("deleted successfully");

        d = true;     
        
      })
      .catch(function(error) {
        //console.log(error);
      });

      console.log(d);
      if(d){
      //do something
      }

Open in new window


when i'm logging the d it is false even after the file was deleted. so if i need a sort of a flag outside of the promise.
0
Comment
Question by:derrida
  • 6
  • 4
  • 3
  • +1
16 Comments
 
LVL 82

Expert Comment

by:leakim971
Comment Utility
in your example, d gonna false everytime

See if that can solve your issue :

      var d = false;

      // // Delete the file
      LimageRef.delete().then(function() {
        //console.log("deleted successfully");
        d = true;
        doSomething(d);        
      })
      .catch(function(error) {
        //console.log(error);
      });

      var doSomething = function(param)
           console.log(param);
           if(param){
              //do something
           }
     }

Open in new window

0
 
LVL 51

Assisted Solution

by:Julian Hansen
Julian Hansen earned 150 total points
Comment Utility
From your last statement it seems you do not fully understand what promises are and how they work (https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Promise)

You can't do anything with d directly. A promise can resolve at any time - so you need to link the resolve on the promise to the action you want to achieve.

In Angular your model represents the state of the application. So you link your interface to state and you set your state in the resolution of the promise.

What you need to do is wrap your delete inside a promise

var d = new Promise(function(resolve, reject) {
  LimageRef.delete().then(function() {
       resolve(true);
  })
  // If there is an error condition then resolve(false);
});

d.then(function(state) {
   if (state) {
    console.log('File deleted');
  }
  else {
   console.log('Error deleting file');
  }
});

Open in new window

0
 
LVL 51

Expert Comment

by:Julian Hansen
Comment Utility
Having said that - the delete() operation is itself a promise - so you should really be working on that only
LimageRef.delete().then(function() {
  // Set state / perform action here
})

Open in new window

0
 
LVL 82

Expert Comment

by:leakim971
Comment Utility
image promise like a setTimeout :

var d = false;
setTimeout(function() {
  // deleting lot of files ^^
  d = true;
}, 1000 * 1000); // 1000 seconds !!!

alert(d); d will be true after 1000s not before !!!

Open in new window

0
 
LVL 27

Expert Comment

by:BigRat
Comment Utility
Obviously from the code LimageRef.delete() implements the Promise API. Thus any further action MUST be performed in the functions defined by the then() of catch() procedures. And wrapping a promise API in a Promise API achieves nothing. The real problem arises when one has to execute a series of connected async functions so that they run in a sequence, but that might be outside the scope of this question.
0
 
LVL 1

Author Comment

by:derrida
Comment Utility
so first thanks. I have read Julian link, and i'm not sure if race is the way to go.
in any case the situation is like so: I am using firebase database to store libraries, and firebase storage to store a library associated image. all that works. now when deleting a library I want to delete the associated image as well. so this is the whole method:
  deleteLib(id: string){
    console.log(id);

      let thisLibImagePath = this.libService.getLibImagePlace(id);

      var libsStorageRef = firebase.storage().ref('images/libs/');

      // Create a reference to the image we need to delete
      var LimageRef = libsStorageRef.child(thisLibImagePath);
      
      var that = this;

      // // Delete the file
      LimageRef.delete().then(function() {
        //the file was deleted from firebase storage
        //now delete the library from firebase database
        //console.log("deleted successfully");
        that.libraries.remove(id); // delete the library  
        
      })
      .catch(function(error) {
        //console.log(error);
      });



  }

Open in new window


how should I go about it ?
0
 
LVL 51

Expert Comment

by:Julian Hansen
Comment Utility
Is the problem here your code is not working or you need to know how to finish it off?

Without seeing the rest of your code I am guessing here

that.libraries.remove(id); // delete the library  

Open in new window


Should this not be

that.libraries.$remove(id); // delete the library  

Open in new window


Where is libraries defined - I am assuming this is a FirebaseArray?
0
 
LVL 1

Author Comment

by:derrida
Comment Utility
well, the file is deleted and also the library. but I get this error when I click the delete button
the error in the console
imagePlace is in the database as the field that store the place the image is in the firebase storage. maybe i'm not reading the error correctly?
0
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 51

Expert Comment

by:Julian Hansen
Comment Utility
So you click delete - that deletes the file and the Firebase entry - but you also get the error? Is that correct?
0
 
LVL 1

Author Comment

by:derrida
Comment Utility
Yes.
0
 
LVL 51

Expert Comment

by:Julian Hansen
Comment Utility
The remove() method should take a Firebase ref - is that what you are passing to deleteLib or are you passing the ID of the record?

When you do the remove() rather get a ref to the node you want to remove and then call remove with that ref as the parameter.
0
 
LVL 82

Accepted Solution

by:
leakim971 earned 350 total points
Comment Utility
somewhere you doing something like : some_variable.imagePlace
but some_variable is null, undefined
0
 
LVL 27

Expert Comment

by:BigRat
Comment Utility
Since the uncaught exception occurs at SafeSubscriber.next in libraries.service.ts then either you are using the API incorrectly or there is something in the service module. Is libraries.service.ts your TypeScript file or does it come along with FireBase?
0
 
LVL 51

Expert Comment

by:Julian Hansen
Comment Utility
It's not Firebase - Angularfire is the Firebase library that would be used here.
0
 
LVL 27

Expert Comment

by:BigRat
Comment Utility
Then we shall need to see the file libraries.service.ts.
0
 
LVL 1

Author Closing Comment

by:derrida
Comment Utility
indeed in my service i was extracting from a snapshot the imagePlace and that caused the error. now i delete libraries and their associated images without any error.
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

This article shows how to create and access 2-dimensional arrays in JavaScript.  It includes a tutorial in case you are just trying to "get your head wrapped around" the concept and we'll also look at some useful tips for more advanced programmers. …
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now