Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow async/await syntax in code snippets #398

Merged
merged 1 commit into from
Mar 23, 2023

Conversation

nimatrueway
Copy link

pretty useful to be able to run asynchronous functions and collect data to pass to a request

pretty useful to be able to run asynchronous functions and collect data to pass to a request
@AnWeber
Copy link
Owner

AnWeber commented Feb 21, 2023

@nimatrueway I have small problems with this change. Just so we are on the same level. Async Script is already possible now. You just have to make sure that a Promise is exported (see https://httpyac.github.io/guide/scripting.html#async-await-or-promise).

{{
  async function wait(){
    const date = new Date();
    await sleep(10000);
    const afterDate = new Date();
    return afterDate.getTime() - date.getTime();
  }
  exports.wait = wait();
}}
GET https://httpbin.org/anything?delay={{wait}}

Your PR implements only top level await. The script emulates/ is actually a CommonJS script where top level await is normally not supported (nodejs/node#21267). Several arguments against top level await do not apply here, but nevertheless I had decided against this implentation so far.

@AnWeber
Copy link
Owner

AnWeber commented Mar 1, 2023

I have now finally decided against it. This PR would make it difficult for me to support ESM scripts in the future. Sorry, but I find there is an acceptable workaround.

@AnWeber AnWeber closed this Mar 1, 2023
@nimatrueway
Copy link
Author

I have now finally decided against it. This PR would make it difficult for me to support ESM scripts in the future. Sorry, but I find there is an acceptable workaround.

Fair enough, thanks for looking into PR Andres.

Top level await is supported by nodejs and the latest version of most browsers, in general it would be nice to have that here too.
https://stackoverflow.com/questions/46515764/how-can-i-use-async-await-at-the-top-level

@AnWeber
Copy link
Owner

AnWeber commented Mar 23, 2023

I let myself be convinced. More people have already asked for this feature and if it really does cause problems, it's a breaking change. Thanks for the improvement, let's see where it goes.

@AnWeber AnWeber merged commit b6e2698 into AnWeber:main Mar 23, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants