B4J Question [BANAno] [SOLVED] Currently GetFileAsDataURL has a return type of BANanoPromise, can it return BANanoFetch?

Mashiane

Expert
Licensed User
Longtime User
Hi there

Interesting....

The GetFileAsDataURL and other GetFile methods return a BANanoPromise and it has a BANanoFetchOptions variable passed to it. Am I right to assume that its possible for it to have a return type of BANanoFetch?

1632014707499.png


This will enable one to execute something like

B4X:
Dim bf As BANanoFetch = BANanoGetFileAsDataURL1(sPath, null)
bf.Return(fr.Blob)

#justAThought..
 
Solution
You are not using the correct methods. e.g. GetFileAsDataURL retrieves the file and makes a DataURL from the result, GetFileAsText will convert the result to plain text etc... They are shortcut methods that do all the heavy lifting IF you know what you are about to receive.

For example, if you know that "tryupload.txt" is of the type text/plain you could do simply this (it will work in this case as the content size = file size):

B4X:
    Dim blob As BANanoObject = BANano.Await(BANano.GetFileAsText("./assets/tryupload.txt", Null, "UTF-8"))
      
    Dim f As BANanoObject
    f.Initialize2("File",Array(Array(blob), "upload.txt", CreateMap("type": "text/plain")))
  
    Log(f.GetField("name"))
    Log(f.GetField("size"))...

Mashiane

Expert
Licensed User
Longtime User
I am testing with this code... and are able to see the blog on the console

Source:

B4X:
const blob = await (await fetch(dataURL)).blob();

How do I make it work?

B4X:
'convert dataURL to blob
Sub dataURLToBlobWait(sPath As String) As BANanoObject
    Dim bf As BANanoFetch
    Dim fr As BANanoFetchResponse
    Dim blob As BANanoObject
    '
    Dim dataUrl As String
     Dim dataUrlProm As BANanoPromise = BANano.GetFileAsDataURL(sPath, Null)
    dataUrlProm.Then(dataUrl)
        bf.Initialize(dataUrl, Null)
           bf.Then(fr)
        bf.Return(fr.Blob)
        bf.Then(blob)
        Log(blob)
        Return blob
        bf.End
    dataUrlProm.End
End Sub

Usage..
B4X:
Dim af As BANanoObject = BANano.Await(dataURLToBlobWait("./assets/tryupload.txt"))
Log("outside...")
Log(af)

1632017291040.png



I am missing something as I'm able to log it internally inside the Fetch, but its "outside" fires before the rest of the code finishes?

Can you please advise where I'm confusing issues?

Thanks
 

Attachments

  • 1632016648077.png
    1632016648077.png
    17.3 KB · Views: 124
Last edited:
Upvote 0

alwaysbusy

Expert
Licensed User
Longtime User
You are not using the correct methods. e.g. GetFileAsDataURL retrieves the file and makes a DataURL from the result, GetFileAsText will convert the result to plain text etc... They are shortcut methods that do all the heavy lifting IF you know what you are about to receive.

For example, if you know that "tryupload.txt" is of the type text/plain you could do simply this (it will work in this case as the content size = file size):

B4X:
    Dim blob As BANanoObject = BANano.Await(BANano.GetFileAsText("./assets/tryupload.txt", Null, "UTF-8"))
      
    Dim f As BANanoObject
    f.Initialize2("File",Array(Array(blob), "upload.txt", CreateMap("type": "text/plain")))
  
    Log(f.GetField("name"))
    Log(f.GetField("size"))
    Log(f.GetField("type"))
    Log(f.GetField("lastModifiedDate"))
    Log(f.GetField("lastModified"))
  
    Log("Done")

It is however better to use something more generic like this, so you can load all kind of files:
B4X:
private Sub FetchAsFile(path As String, name As String) As BANanoObject
    Dim fetch As BANanoFetch
    Dim fetchResponse As BANanoFetchResponse
    Dim blob As BANanoObject
  
    Dim prom As BANanoPromise
  
    ' we are going to use a Promise Wrapper as we want to use Await() for it.
    prom.NewStart
        fetch.Initialize(path & "/" & name, Null)
        fetch.Then(fetchResponse)
            ' resolve the blob
            Return fetchResponse.Blob
        fetch.Then(blob) 'ignore
            ' Use ReturnThen/ReturnElse for the final result in case of a Promise.NewStart/NewEnd wrapper
            BANano.ReturnThen(blob)
        fetch.End  
    prom.NewEnd
  
    ' wait from the Promise
    Dim result As BANanoObject = BANano.Await(prom)
  
    ' make a new File object
    Dim f As BANanoObject
    f.Initialize2("File",Array(Array(result), name, CreateMap("type": result.getfield("type"))))
      
    Return f
End Sub

Usage:
B4X:
    ' here the type is a png file
    Dim f As BANanoObject = BANano.Await(FetchAsFile("./assets","large.png"))
  
    Log(f.GetField("name"))
    Log(f.GetField("size"))
    Log(f.GetField("type"))
    Log(f.GetField("lastModifiedDate"))
    Log(f.GetField("lastModified"))
  
    Log("Done")

Alwaysbusy
 
Upvote 1
Solution

Mashiane

Expert
Licensed User
Longtime User
Just a quick one, I have always assumed that your sub name should end with Wait if you will use Banano.Await.

Is that a wrong assumption or perhaps when is it compulsory or not or not needed? What are the scenarios?

Anyway I will run in debug the js to see how it's showing, I'm strill trying to figure out all these async await and their Banano equivalents. Phew.

It's the finding the Javascript code and trying to translate it the banano way that becomes the challenge.

Sometimes using #if Javascript #end if is out of the question, it's that Banano way of doing things, so much to learn..

Thanks
 
Upvote 0
Top