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

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.
LVL 1
derridaAsked:
Who is Participating?
 
leakim971Connect With a Mentor PluritechnicianCommented:
somewhere you doing something like : some_variable.imagePlace
but some_variable is null, undefined
0
 
leakim971PluritechnicianCommented:
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
 
Julian HansenConnect With a Mentor Commented:
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
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
Julian HansenCommented:
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
 
leakim971PluritechnicianCommented:
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
 
BigRatCommented:
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
 
derridaAuthor Commented:
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
 
Julian HansenCommented:
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
 
derridaAuthor Commented:
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
 
Julian HansenCommented:
So you click delete - that deletes the file and the Firebase entry - but you also get the error? Is that correct?
0
 
derridaAuthor Commented:
Yes.
0
 
Julian HansenCommented:
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
 
BigRatCommented:
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
 
Julian HansenCommented:
It's not Firebase - Angularfire is the Firebase library that would be used here.
0
 
BigRatCommented:
Then we shall need to see the file libraries.service.ts.
0
 
derridaAuthor Commented:
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
All Courses

From novice to tech pro — start learning today.