Probably Promise overuse?

api

#1

Don’t you get an impression, that returning Permissions from API calls is not always justified? Aren’t there functions that do not require network calls and are just IPC calls between main and renderer processes that are immediate because they happen within the same machine? For example safeMutableDataPermissionsSet.setAllow or safeCrypto.generateEncKeyPair. Do they call the network? I think there should be as many synchronous calls as possible. They are much more simple to use and do not leave us with “then” on every code line.

As to “thens”:

https://github.com/maidsafe/safe_examples/blob/master/email_app/app/safenet_comm.js#L221

… do you really have to create a function object for a simple assignment operation, inboxMd = md etc.?


#2

I share your opinion, there seem to be some calls that seemingly have little reason to be asynchronous, like CryptoInterface.sha3Hash. It probably has something to do with how the SAFE client library is called using a Foreign Function Interface (FFI).

By the way, you can rewrite the then chain to make use of async and await:

(async () => {
    let md      = await app.mutableData.newRandomPublic(CONSTANTS.TAG_TYPE_INBOX)
    let inboxMd = await md.quickSetup(baseInbox)
    let permSet = await app.mutableData.newPermissionSet()
    await permSet.setAllow('Insert')
    await inboxMd.setUserPermissions(null, permSet, 1)
})()

Should do the exact same thing – didn’t test.


JavaScript tips for using the SAFE API
#3

This sounds fantastic but unfortunately it doesn’t seem to work in Beaker. It throws a syntax error.


#4

@bzee just try to declare a function with async:

async function test() {};

Throws the following error when page loads:

Uncaught SyntaxError: Unexpected token function


#5

Interesting, is that the whole snippet? Where are you executing that JavaScript? Beaker has been executing it fine for me.

If you’re targeting an older system, then I recommend using a transpiler (e.g. Babel).


#6

Well, I don’t even execute it. The error occures right after the page loads. My whole html is a script tag containing the above js line.

Which version of SAFE browser do you use? Mine is 0.4.0

What do you mean by this? I use Win 10 with SAFE browser (compiled by maidsafe) and a simple html file.
EDIT: In chrome this works without problems. @joshuef any thoughts?


#7

I know it’s ES2017, and therefore fairly experimental, but modern browsers should be handling it fine. I find the error message that you get a little weird, as you’d expect it to complain about the async token, not about function. I’ve been running it on a slightly more recent version (7dbcf86).

I can’t get 0.4.0 to work on a Windows 10 environment to replicate your issue (using npm install):

> safe-app@0.1.0-dev.1 postinstall C:\Users\benno\dev\src\safe_browser-0.4.0\app\node_modules\safe-app
> download_deps --package package.json

Downloading tmp-9208-0-system_uri-1.0.0-pre-win-x64.zip
Error: GET https://github.com/gnunicorn/system_uri/releases/download/v/system_uri-1.0.0-pre-win-x64.zip returned 404

Notice the wrong URL (/v/ part). The build process goes fine until the download_deps command is issued.


#8

This is probably the reason for overuse of async api:


#9

Async await is a cool feature. It is supported from 7.10.0 nodejs or above I guess. The browser previously was compatible only with 6.9 of nodejs. This will be supported once we update to the nodejs version from which async/await can be supported.

Edit: my comment is related to the usage of async await in DOM layer


#10

@Krishna, are you sure the NodeJS version influences the support of the Beaker browser? I have been using async/await just fine, even though I compiled the SAFE browser with 6.5.0:


#11

Have not tried it on DOM. I said that based on the feature list from node.green. When I used to do some work on browser code, it never compiled with async/await. I will check it out when I get sometime @bzee.


#12

I’ve opened an issue so you can test / track this when building for Windows:


#13

Running Windows 10 and a build of safe_browser master branch.

Async is working fine in the console.

I’ll try it on, https://github.com/maidsafe/safe_browser/releases/download/0.4.0/safe-browser-v0.4.0-win-x64.zip, as well


#14

@Mindphreaker, @bzee, @hunterlester the core electron dependency was updated in a version later than 0.4.0, which I think @bzee is using, which I suspect brings the async functionality. Prior to this we were on a really old version of electron.


#15

Sounds great, looking forward to the upcoming release! :slight_smile:


#16