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

Mashiane

Expert
Licensed User
Longtime User
Hi there

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

dataurlafter.png


B4X:
#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?
 

Attachments

  • Promises.zip
    1.6 KB · Views: 304

Daestrum

Expert
Licensed User
Longtime User
I dont really know javascript but try
B4X:
#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
 
Upvote 0

Kiffi

Well-Known Member
Licensed User
Longtime User
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.

this is the "BANano-Way":
B4X:
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
 
Upvote 0

Mashiane

Expert
Licensed User
Longtime 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:
Upvote 0

alwaysbusy

Expert
Licensed User
Longtime User
I just answered the original posters question which referred to javascript console.log.
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:
Upvote 0
Top