6

In JS in can write a self invoking arrow function with async like this:

(async () => {
    const promis = fetch(uri);
    console.log(await promis);
})();

A self invoking function without parameters I could also write like this:

{
   // do something
}

I was asking myself, is there a syntax to combine both and do something like this or is the first example already the shortest form?

// this is not working
async {
   const promis = fetch(uri);
   console.log(await promis);
}
2
  • 7
    Your second snippet is not a self invoking function without parameters, it's just a block. For instance, a var declaration won't be scoped inside of your second example Commented Jul 14, 2018 at 14:42
  • async attributes a function, so the initial syntax is as terse as it can get. Commented Jul 14, 2018 at 14:42

3 Answers 3

11

two way, we just can short a little :)

  1. simple way
!async function () {
    console.log("e",'yibu');
}();

or like yours

(async  () => {
    console.log("e",'yibu');
})();

//maybe this is better then above
;(async function () {
    console.log("e",'yibu');
}());

//this is allmost same
;[ async function () {
    console.log("e",'yibu');
}()];
  1. use [then] this is not absolute "anonymous"
var x=async  () => 100;

x().then(
    e=>console.log({e})
);
Sign up to request clarification or add additional context in comments.

2 Comments

What's the name of the syntax in your simple example? I haven't come across that before.
function expression, the [!] means that not only definition, that is an expression. expression means some thing that can be run, and that need to be a value. other way, we can call it: closure, or: IIFE (immediately invoked function expression)
3

In the Javascript syntax, async is a modifier of a function. So, the only thing that can be async is a function.

Your second section of code is merely a block. It doesn't create a new function or a new function scope. And, per the Javascript syntax, you can't use async with a block. For example a variable declared in that block with var would still be hoisted to the top of the containing function scope because that doesn't create a new function scope.

Your third section of code does not work because async only works with functions, not with blocks (per the Javascript specification).

If you want an inline async section of code, you have to declare and execute a function and your first code block is a compact way to do that. You have to have a function, not just a block.

Comments

2

No, you cannot. A block statement only creates a new scope, just like an IIFE. An async function however is executed asynchronously, that can't be done with a block.

1 Comment

Saying an async function isn't executed asynchronously is kind of misleading. When you call an async function, it is executed synchronously (up to the first await) and then returns a promise. In fact, if there's no await in it, it's executed entirely synchronously.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.