# Do Expression with Alternative Syntax - Immediately Invoked Arrow Functions

**URL:** <https://es.discourse.group/t/do-expression-with-alternative-syntax-immediately-invoked-arrow-functions/1173>\
**Category:** 💡 Ideas\
**Created:** [January 22, 2022, 1:57am UTC](https://es.discourse.group/t/do-expression-with-alternative-syntax-immediately-invoked-arrow-functions/1173 "2022-01-22T01:57:53Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![EdSaleh](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/edsaleh/32/1198_2.png) [@EdSaleh](https://es.discourse.group/u/EdSaleh)\
**Post date:** [January 22, 2022, 1:57am UTC](https://es.discourse.group/t/do-expression-with-alternative-syntax-immediately-invoked-arrow-functions/1173/1 "2022-01-22T01:57:53Z")

</div>

# Introduction

**Hello,**

**I had an idea to allow returns from statements and decided to do a research and found Do Expression proposal very similar and more complex than my idea.**

**My idea is basically to create a new keyword syntax as a shortcut for an executed arrow function. It behaves same as arrow function.**

# Examples:

```ts
const 
 a=1,
 b = do if(a) return 1; else return 0;;

```

# Transpilation:

```ts
const 
 a=1,
 b = (()=> {if(a) return 1; else return 2})();

```

**We can also use alternative names or keywords for this new feature.**

# Examples:

- -\>
- =\> (preceded by an expression and followed by a block or a statement)

```ts
const 
 a=1,
 b = -> if(a) return 1; else return 0;;

```

```ts
const 
 a=1,
 b = => if(a) return 1; else return 0;;

```

Or

```ts
const 
 a=1,
 b = =>{ if(a) return 1; else return 0;};

```

### We might call `=>` syntax a _Immediately Executed Arrow Function_ or _Immediately Invoked Arrow Function_

**We can even pass parameters as an object:**

```ts
const 
 a=1,
 b = {value:a} => if(value) return value; else return 0;

```

## Match Example:

```ts
const a=1,b=2;
const c = 
    {a,b}=> {
        if(a) return a; 
        else if(b) return b; 
        else return 0;
    }
console.log(c)

```

## Pipe and Chaining Example

**Consecutive chains inherit the single parameter of single field object of parent chain, each assigning return value to that single field object property and passing that as parameters to next chain. We can enforce rule for my proposal to only have one object with one field as the parameter, that field can be be assigned as any object of course.**

```ts
const value=1;
const c = {value} 
    => value+1
    => value+1
    => value+2
console.log(c) //5

```

_Alternative Syntax:_

```ts
const c = value
    -> value+1
    -> value+1
    -> value+2
console.log(c) //5

```

We can use for chaining ==\> syntax to avoid specifying the parameter with the method:  
`{value} ==> Filter ==> Map` = `{value} => Filter(value) => Map(value)`

**We can of course discuss the syntax, my proposal has 3 suggestions: =\>, -\>, do. The reason I prefer =\> is because my method is actually based on anonymous method, just immediately executed, and allowing flexibility.**

Thank you,

---

<div class="post-metadata">

**Author:** ![EdSaleh](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/edsaleh/32/1198_2.png) [@EdSaleh](https://es.discourse.group/u/EdSaleh)\
**Post date:** [January 22, 2022, 2:05am UTC](https://es.discourse.group/t/do-expression-with-alternative-syntax-immediately-invoked-arrow-functions/1173/2 "2022-01-22T02:05:18Z")

</div>

> <https://github.com/tc39/proposal-do-expressions/issues/74>
>
> \# Introduction
> \*\*Hello,\*\*
> 
> \*\*I had an idea to allow returns from statements a…nd decided to do a research and found Do Expression proposal very similar and more complex than my idea.\*\*
> 
> \*\*My idea is basically to create a new keyword syntax as a shortcut for an executed arrow function. It behaves same as arrow function.\*\*
> 
> \# Examples:
> \`\`\`ts
> const 
> a=1,
> b = do if(a) return 1; else return 0;;
> \`\`\`
> 
> \# Transpilation:
> 
> \`\`\`ts
> const 
> a=1,
> b = (()=\> {if(a) return 1; else return 2})();
> \`\`\`
> 
> \*\*We can also use alternative names or keywords for this new feature.\*\*
> \# Examples:
> \- -\>
> \- =\> (preceded by an expression and followed by a block or a statement)
> 
> \`\`\`ts
> const 
> a=1,
> b = -\> if(a) return 1; else return 0;;
> \`\`\`
> \`\`\`ts
> const 
> a=1,
> b = =\> if(a) return 1; else return 0;;
> \`\`\`
> Or
> \`\`\`ts
> const 
> a=1,
> b = =\>{ if(a) return 1; else return 0;};
> \`\`\`
> \### We might call \`=\>\` syntax a \*Immediately Executed Arrow Function\*
> 
> \*\*We can even pass parameters as an object:\*\*
> \`\`\`ts
> const 
> a=1,
> b = {value:a} =\> if(value) return value; else return 0;
> \`\`\`
> 
> \## Match Example:
> \`\`\`ts
> const a=1,b=2;
> const c = 
> {a,b}=\> {
> if(a) return a; 
> else if(b) return b; 
> else return 0;
> }
> console.log(c)
> \`\`\`
> \## Pipe and Chaining Example
> \*\*Consecutive chains inherit the single parameter of single field object of parent chain, each assigning return value to that single field object property and passing that as parameters to next chain. We can enforce rule for my proposal to only have one object with one field as the parameter, that field can be be assigned as any object of course.\*\*
> \`\`\`ts
> const value=1;
> const c = {value} 
> =\> value+1
> =\> value+1
> =\> value+2
> console.log(c) //5
> \`\`\`
> \*Alternative Syntax:\*
> \`\`\`ts
> const c = value
> -\> value+1
> -\> value+1
> -\> value+2
> console.log(c) //5
> \`\`\`
> We can use for chaining ==\> syntax to avoid specifying the parameter with the method:
> \`{value} ==\> Filter ==\> Map\` = \`{value} =\> Filter(value) =\> Map(value)\`
> 
> \*\*We can of course discuss the syntax, my proposal has 3 suggestions: =\>, -\>, do. The reason I prefer =\> is because my method is actually based on anonymous method, just immediately executed, and allowing flexibility.\*\*
> 
> Thank you,

> **[GitHub - EdSaleh/proposal-immediately-executable-arrow-functions-expression](https://github.com/EdSaleh/proposal-immediately-executable-arrow-functions-expression)**
>
> Contribute to EdSaleh/proposal-immediately-executable-arrow-functions-expression development by creating an account on GitHub.

---

<div class="post-metadata">

**Author:** ![ljharb](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/ljharb/32/8_2.png) [@ljharb](https://es.discourse.group/u/ljharb)\
**Post date:** [January 22, 2022, 2:11am UTC](https://es.discourse.group/t/do-expression-with-alternative-syntax-immediately-invoked-arrow-functions/1173/3 "2022-01-22T02:11:46Z")

</div>

`=>` is not an option, because it strongly suggests it will work like an arrow function - but the point of do-expressions is to not require a function call, not add a stack frame, etc, so "sugar for an immediately invoked arrow function" wouldn't achieve that goal.

`do` without curly braces i believe would be ambiguous, both for parsers and more importantly, for humans.

Your suggested transpilation wouldn't work if the do expression contained break, continue, yield, await, new.target, etc.

---

<div class="post-metadata">

**Author:** ![EdSaleh](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/edsaleh/32/1198_2.png) [@EdSaleh](https://es.discourse.group/u/EdSaleh)\
**Post date:** [January 22, 2022, 4:10am UTC](https://es.discourse.group/t/do-expression-with-alternative-syntax-immediately-invoked-arrow-functions/1173/4 "2022-01-22T04:10:54Z")

</div>

Hi @ljharb, Thanks for input, I just posted the issue here as well for input from other and future reference and discussion.

---

<div class="post-metadata">

**Author:** ![EdSaleh](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/edsaleh/32/1198_2.png) [@EdSaleh](https://es.discourse.group/u/EdSaleh)\
**Post date:** [January 22, 2022, 4:12am UTC](https://es.discourse.group/t/do-expression-with-alternative-syntax-immediately-invoked-arrow-functions/1173/5 "2022-01-22T04:12:00Z")

</div>

Thank you,

---

<div class="post-metadata">

**Author:** ![DiriectorDoc](https://yyz2.discourse-cdn.com/free1/user_avatar/es.discourse.group/diriectordoc/32/987_2.png) [@DiriectorDoc](https://es.discourse.group/u/DiriectorDoc)\
**Post date:** [January 22, 2022, 8:55pm UTC](https://es.discourse.group/t/do-expression-with-alternative-syntax-immediately-invoked-arrow-functions/1173/6 "2022-01-22T20:55:52Z")

</div>

Proposed alternate syntax:

```javascript
let foo = do function(){
    return "bar"
};
let baz = do () => "qux";

console.log(foo) // "bar"
console.log(baz) // "qux"

```

This I think is nicer, but even so, a little unnecessary. To me, this would be identical to `(() => "qux")()`. I admit, the `do` syntax is a _lot_ cleaner, but it raises a few questions.

How would this work?

```javascript
do someFunction

```

Here, there is hardly an advantage to using `do` than simply `()`.

And there's also an issue with returning values. In a `do`-`while` loop, it will end the loop AND the function.

```javascript
function func1(){
    do {
        return "value"
    } while(true);
    console.log("Unreached")
}
function func2(){
    let a = do return "value"; // Using your proposed syntax
    console.log("Reached")
}

func1() // "Unreached" is never logged
func2() // What happens here?

```

In my opinion, this seems like it could cause a lot of confusion. Though I would like to see some cleaner method for creating IIFEs, I don't think this is the way to do it.
