B4J Question [BANano]: [SOLVED] How to RunJavascriptMethod and wait for it to finish?

Discussion in 'B4J Questions' started by Mashiane, Feb 2, 2019.

  1. Mashiane

    Mashiane Expert Licensed User

    Hi there

    I'm trying to get a file's DataURL using a promise.

    dataurlafter.png

    Code:
    #if javascript
        function getdataurl(fileid){
            var fi = document.getElementById(fileid);
            var file = fi.files[0];
            var geturl = new Promise(
                function(resolve, reject){
                    var fr = new FileReader(); 
                    fr.onload = function(){
                        data = fr.result;
                          resolve(data);
                    };
                    fr.readAsDataURL(file);
                  }
            );
            geturl.then(function(result) {
                console.log(result);
                  return result;
            });
        }
    #End If
    Thing is, the console.log(result); here runs after the result of 'undefined' is provided.

    I have attached herein an example project. Is it possible to execute a javascript method and wait for it to finish? I want to save the returned DataURL to a database as a base 64 string.

    Thanks in advance?
     

    Attached Files:

    joulongleu likes this.
  2. Daestrum

    Daestrum Well-Known Member Licensed User

    I dont really know javascript but try
    Code:
    #if javascript
     async function getdataurl(fileid){
      var fi = document.getElementById(fileid);
      var file = fi.files[0];
      var geturl = new Promise((resolve, reject) => {
           var fr = new FileReader(); 
           fr.onload = function(){
         var data = fr.result;
         if (data==="") reject("No Data"); // I just check for 0 length data
              resolve(data);
           };
           fr.readAsDataURL(file);
      });
      try{
       var result = await geturl;
       console.log(result);
        } catch(err) {
       console.log("catch error : " + err);
       return null;
      }
      return result;
     }
    #End If
     
    alwaysbusy, Mashiane and joulongleu like this.
  3. Kiffi

    Kiffi Active Member Licensed User

    this is the "BANano-Way":
    Code:
    Sub Init()
        BANAno.GetElement(
    "#body").Empty
        BANAno.GetElement(
    "#body").Append($"<input id="fu" type="file"></input>"$)
        BANAno.GetElement(
    "#fu").HandleEvents("change", Me, "globalevent")
    End Sub

    Sub UploadCallback(Event As Map)
        
        
    Dim Target As BANanoObject = Event.Get("target")
        
        
    Dim UploadedFile As BANanoObject = Target.GetField("file")
        
        
    Log("UploadedFile: " & UploadedFile.GetField("name"))
        
    Log("DataURL: " & Target.GetField("result"))
        
    End Sub

    Sub globalevent(event As BANanoEvent)
        
        
    Select Case event.ID
            
            
    Case "fu"

                
    Dim FU As BANanoElement = BANAno.GetElement("#fu")
            
                
    Dim UploadedFiles() As String = FU.ToObject.GetField("files").Result
            
                
    For UploadCounter = 0 To UploadedFiles.Length - 1
            
                    
    Dim FileReader As BANanoObject
                    FileReader.Initialize2(
    "FileReader"Null)
                    FileReader.SetField(
    "file", UploadedFiles(UploadCounter))
                    
                    
    Dim evt As Map
                    FileReader.SetField(
    "onload", BANAno.CallBack(Me, "uploadcallback"Array(evt)))
                    FileReader.RunMethod(
    "readAsDataURL", UploadedFiles(UploadCounter))
            
                
    Next
            
        
    End Select
        
    End Sub
    Greetings ... Peter
     
    joulongleu, Mashiane and alwaysbusy like this.
  4. alwaysbusy

    alwaysbusy Expert Licensed User

    Very nice snippet @Kiffi!

    A lot more educational than just trying to run some JavaScript one finds on the internet. Excellent example of the use of BANanoElements and BANanoObjects.
     
    joulongleu likes this.
  5. Daestrum

    Daestrum Well-Known Member Licensed User

    I just answered the original posters question which referred to javascript console.log.
     
    joulongleu, alwaysbusy and Mashiane like this.
  6. Mashiane

    Mashiane Expert Licensed User

    Thanks a million @Kiffi. Very much appreciated, this will solve my problem much. All I was interested in was finding my solution for my problem. Yes, because BANano is quite very new compared to javascript that has been in existence for millenium, these kinda of challenges or porting existing javascript to solve problems will always suffice.

    You are a star! and please dont stop sharing your knowledge here its much eye opening, as much as at the current moment honestly, I dont even understand what you have just done. We will ask questions and I think that is rather expected from newbies across the board.

    Kind Regards

    Mashy
     
    Last edited: Feb 2, 2019
    joulongleu likes this.
  7. Mashiane

    Mashiane Expert Licensed User

    Don't worry @Daestrum , the FACT is, my problem is solved that's all I needed, the rest of the sarcasm is always something that happens here and we glady ignore.
     
    joulongleu likes this.
  8. alwaysbusy

    alwaysbusy Expert Licensed User

    Absolutely! I'm just pointing out to the OP that 'learning' how BANano works is a lot more profitable for everyone on the long run. BANano is still evolving, and maybe one day the whole '#if Javascript' will be depreciated because BANano can do everything, hence breaking all such 'solutions'.

    The OP clearly is not a person who wants to learn and prefers copy-and-paste without understanding it (hence his mistake in the javascript in the first place) because it suffices. No help from me anymore to him then as he 'gladly' ignores everything I say anyway. Time to activate the forums ignore function...
     
    Last edited: Feb 2, 2019
    joulongleu likes this.
  9. Mashiane

    Mashiane Expert Licensed User

    joulongleu likes this.
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice