Skip to content
This repository has been archived by the owner on Aug 2, 2022. It is now read-only.

Async and Await #67

Open
cdrchops opened this issue May 21, 2021 · 5 comments
Open

Async and Await #67

cdrchops opened this issue May 21, 2021 · 5 comments

Comments

@cdrchops
Copy link

I have a bunch of groovy code I'm converting using grooscript. However, I was wondering if there's a way to add async and await to the scripts in some way. I know I could probably add a metaclass item and try that. I just didn't know if anyone had thought about this as an addition.

Right now, i'm having to edit the converted files directly to add async and await. Which, along these lines I also have fetch to include.

current js file:
async function checkWord(word) { var url =/jsonlookup/${word}`;

let response = await fetch(url);

let content;

if (!response.ok) {
    throw new Error(`HTTP error! status: ${response.status}`);
}

content = await response.text();

return content;

}

async function lookupWordInCED(word) {
var content = await checkWord(word);

let values = await Promise.all([content]);

if (values.length > 0 && values[0] !== "null") {
    return values;
} else {
    return [];
}

}
`

Here's the grooscript code I'm modifying:
AjaxCall.checkWord = async function (word) { var url = http://localhost:8080/jsonlookup/${word}`;

let response = await fetch(url);

let content;

if (!response.ok) {
    throw new Error(`HTTP error! status: ${response.status}`);
}

content = await response.text();

return content;

}

AjaxCall.lookupWordInCED = async function(word) {
var content = await AjaxCall.checkWord(word);

let values = await Promise.all([content]);

if (values.length > 0 && values[0] !== "null") {
    return gs.list(values);
} else {
    return gs.list([]);
}

}`

obviously, ideally, I'd like to make the javascript calls and promises seamless. I'll read through the groocsript source and see if I can narrow it down some.

Great job and I use this in a lot of my projects.

@chiquitinxx
Copy link
Owner

Hey! Nice :)

Well, we have support to include javascript code in your groovy classes with @GsNative https://www.grooscript.org/doc.html#_gsnative. But I think you need something like create async functions or work with promises. We don't have this kind of code that is converted to something specific in javascript. Maybe you can create annotations to do that, something like annotate methods with a new annotation ¿@jsasync? It seems a good feature you can work on :)

@cdrchops
Copy link
Author

awesome! I'll check that out. thanks!

@cdrchops
Copy link
Author

I was able to make the changes I needed and test.

I built the plugin jar with the reference to the new grooscript jar I made. I then went to include the conversion in my project so I could test against my code and make sure everything was working the way it should be. I'm having issues getting the conversion to load without an error.

Did you do something special to test locally? I've been using Groovy 16 years and 9 years with gradle and this is the first time I've looked at a gradle plugin to include locally in a project. If you have an easier way to test locally I'd appreciate it.

Here are the changes I made locally
settings.gradle
pluginManagement { repositories { mavenLocal() } }

build.gradle (also note my local version is 1.3.2 so I could make sure i wasn't pulling something accidentally
`buildscript {
// classpath files('C:\Users\torr\.m2\repository\org\grooscript\gradle-plugin\1.3.2\gradle-plugin-1.3.2.jar')
repositories { flatDir name: 'libs', dirs: "./build/libs" }

//"org.grooscript.conversion" version "1.3.2"
dependencies {
    classpath 'org.grooscript:grooscript:1.3.2'
    classpath 'org.grooscript.conversion:gradle-plugin:1.3.2'
}

}`

error I get:
`Plugin [id: 'org.grooscript.conversion', version: '1.3.2'] was not found in any of the following sources:

  • Gradle Core Plugins (plugin is not in 'org.gradle' namespace)
  • Plugin Repositories (could not resolve plugin artifact 'org.grooscript.conversion:org.grooscript.conversion.gradle.plugin:1.3.2')
    Searched in the following repositories:
    MavenLocal(file:/C:/Users/torr/.m2/repository/)`

I didn't know if you'd ran into anything like this before

thanks

@chiquitinxx
Copy link
Owner

Hey! To test locally I can recommend staying with 1.3.1-SNAPSHOT version, snapshot versions are updated always. Be sure you install both grooscript and plugin snapshots to your local repository (./gradlew build or install, I forgot :)). About the problem with the plugin, maybe can be because you are using plugin { ... } in your build.gradle. I suggest you use the other way to add the dependency, (https://github.com/chiquitinxx/grooscript-plugins/tree/master/gradle-plugin):

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'org.grooscript:gradle-plugin:1.3.0'
    }
}

apply plugin: 'org.grooscript.conversion'

@cdrchops
Copy link
Author

I'll give that a try. I did publish to local - because I'd made changes to grooscript for the JsAsync work - and so I wanted to update the plugin and run the whole conversion. The code looks good and the conversion seems to be fine. I'll let you know. thanks for the help

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants