npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@bacloud22/eager_return_js

v1.3.1

Published

Return values in the call stack as fast as possible

Downloads

11

Readme

Eager return

Context

It is common to call a function to do a task and return results as soon as possible. Sometimes we call a function that calls other functions to look for that same results. In this particular situation, we need to return results to the top most code as soon as possible.

Example

import { give, receive } from "./eager-return.js";
// This is your own function with its subsequent call-stack
// Somewhere in its subsequent call-stack, use `give(value)` 
// instead of `return`
function func(msg1, msg2) {
    console.log(msg1 + ' ' + msg2)
    const retunedValue = func2();
    console.log('this may or may not execute')
    return retunedValue
}

function func2() {
    const constraint = Math.random() > 0.5;
    if (constraint) {
        return 'slow path'; // normal return
    } else {
        give(42); // no return give(42)
    }
    console.log('this will never execute')
}

const result = receive(func, 'hello', 'world');
console.log(result)

Comments

By definition, every function would like to be deterministic and to have full control over its code and memory.
This is to say it is uncommon (generally uneeded) to let the callee function take control over its caller. I think it is unecessary and time wasting to find situations where we need that because of course we could find such situations but it is very uncommon in the mindset of programming.

Nevertheless, I think the example we raised earlier is an exception as it is very common and well defined. It is also well guarded as the callee function doesn't take control of its caller undefinitly, there is the give & recieve agreement.

Proposale

In the context described above, I would like to see a new kind of return defined and added to JavaScript.

I think defining a return that goes through the stack to the top most calling code is unfeasible and/or too confusing. We should think of an agreement between the caller and the callee functions so that the developer could decide clearly and declare this particular situation.

(We already had async function_n(){} with await function_n() added to JavaScript without any problems but with new solutions !)

My propositions is to add two keywords similar as async and await to describe this situation. Something like give and receive (I'm not English native, maybe not the best words).

Edit 1:

  • Visibility this is similar to try,catch/throw in regard of the flow of execution.

Edit 2:

  • (use of try-catch, counter intuitive, as we are using throw error native flow of execution to return values. But it does the job)

@theScottyJam comment on this:

I think you're right about this idea being fairly similar to try/catch/throw. In fact, I don't think it would be too much effort to implement something like this in userland.

Instead of this:

```js
function `give` caller(x, y) { ... }
```

write this:

```js
function caller(give, x, y) { ... }
```

And instead of this:

```js
`found` return42();
```

write this:

```js
give(return42());
```

Then use a utility `receive()` function to capture the eager-return value, and voila! With a small user-land library, you have this feature.

Current solution (this library)

After the discussion, theScottyJam came up with a JS solution (using throw/catch flow as imagined before). It is yet our wish to see it in native JavaScript.

Meanwhile, You can now use the library as the following:

import { give, receive } from "./eager-return.js";
// This is your own function with its subsequent call-stack
// Somewhere in its subsequent call-stack, use `give(value)` 
// instead of `return`
function func(msg1, msg2) {
    console.log(msg1 + ' ' + msg2)
    const retunedValue = func2();
    console.log('this may or may not execute')
    return retunedValue
}

function func2() {
    const constraint = Math.random() > 0.5;
    if (constraint) {
        return 'slow path'; // normal return
    } else {
        give(42); // no return give(42)
    }
    console.log('this will never execute')
}

const result = receive(func, 'hello', 'world');
console.log(result)

Final note

This is a practical solution, but has its limit, because receive function doesn't know that func gives some value somewhere in its call-stack. Using this helper library the developer knows that.

The purpose of the proposal is to introduce this to JavaScript so that receive/give is an agreement, just like await/async is.

License

Author: bacloud(14)[at]gmail(dot)com
Co-author: moussa(dot)tnm51[at]gmail(dot)com
MIT