Solved

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

Posted on 2016-11-15
16
83 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 4
  • 3
  • +1
16 Comments
 
LVL 82

Expert Comment

by:leakim971
ID: 41888096
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 58

Assisted Solution

by:Julian Hansen
Julian Hansen earned 150 total points
ID: 41888110
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 58

Expert Comment

by:Julian Hansen
ID: 41888117
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
AWS Certified Solutions Architect - Associate

This course has been developed to provide you with the requisite knowledge to not only pass the AWS CSA certification exam but also gain the hands-on experience required to become a qualified AWS Solutions architect working in a real-world environment.

 
LVL 82

Expert Comment

by:leakim971
ID: 41888131
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
ID: 41888168
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
ID: 41889180
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 58

Expert Comment

by:Julian Hansen
ID: 41889296
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
ID: 41889328
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
 
LVL 58

Expert Comment

by:Julian Hansen
ID: 41889452
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
ID: 41889535
Yes.
0
 
LVL 58

Expert Comment

by:Julian Hansen
ID: 41889561
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
ID: 41889563
somewhere you doing something like : some_variable.imagePlace
but some_variable is null, undefined
0
 
LVL 27

Expert Comment

by:BigRat
ID: 41890024
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 58

Expert Comment

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

Expert Comment

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

Author Closing Comment

by:derrida
ID: 41890950
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
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…
Suggested Courses
Course of the Month10 days, 13 hours left to enroll

632 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