Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ES8 Immediately invoked async function expression

I haven't seen these constructs used much but I've found myself writing them to make use of async / await in functions that wouldn't typically return a promise, for example

chan.consume(queue, (msg) => {   this.pendingMsgs++; // executed immediately   (async () => {     await this.handleMessage(msg);     this.pendingMsgs--;     if (cancelled && this.pendingMsgs === 0) {        await chan.close();        await this.amqpConnectionPool.release(conn);     }    })(); }); 

as opposed to

chan.consume(queue, async (msg) => { // external lib does not expect a return value from this callback   this.pendingMsgs++;  // executed in promise context(?)   await this.handleMessage(msg);   this.pendingMsgs--;     if (cancelled && this.pendingMsgs === 0) {        await chan.close();        await this.amqpConnectionPool.release(conn);     } }); 

or

chan.consume(queue, (msg) => {   this.pendingMsgs++;  // no await - excess function decls & nesting   this.handleMessage(msg).then(() => {     this.pendingMsgs--;     if (cancelled && this.pendingMsgs === 0) {        chan.close().then(() => {          this.amqpConnectionPool.release(conn);        });     }   }); }); 

Is this 'a thing'? Are there pitfalls here I should be aware of? What's the lowdown on use of async / await in these kind of situations?

like image 968
Drew R Avatar asked Nov 22 '16 15:11

Drew R


People also ask

Can IIFE be async?

An async function expression can be used as an IIFE (Immediately Invoked Function Expression) which runs as soon as it is defined.

How do you await an IIFE?

The await keyword can only be used inside of a function marked with the async keyword. Since the code we're executing is not currently in a function, we'll have to put it inside of one. One way to do this is with an "async IIFE" (immediately invoked function expression)...

Can async function have parameters?

An async function (or AyncFunction) in the context of Async is an asynchronous function with a variable number of parameters where the final parameter is a callback.


1 Answers

Is this 'a thing'?

Yes. It comes up every now and then, e.g. here. They're known as IIAFEs :-)
If you want to put focus on the arrow, you could also call them IIAAFs.

Are there pitfalls here I should be aware of?

Whenever you call a promise-returning function and don't return the result to somewhere else, you are responsible for the promise yourself - which means that you have to handle errors from it. So the pattern should in general look like

(async () => {     … })().catch(err => {     console.error(err); }); 

if you don't want to concern yourself with unhandled-rejection events.

What's the lowdown on use of async/await in these kind of situations?

Not much, compared to the then version. However, you say "the external lib does not expect a return value from this callback", which might hint at the library's incompatibility with asynchronous callbacks, so beware what you are doing when. It also might depend on exceptions being thrown synchronously from the callback, so it all depends on what the library expects here (and if there are no expectations, whether that may change in the future). You don't want future incompatibilities in case the library will start to treat promise return values specially.

However, I would still recommend the second pattern that directly passes the async function directly as the callback because of its better readability. If you want to avoid returning a promise to the library, create a helper function that wraps the callback:

function toVoid(fn) {     return (...args) => void fn(...args); } function promiseToVoid(fn) {     return (...args) => void fn(...args).catch(console.error); } 

which you could use like this:

chan.consume(queue, toVoid(async (msg) => {      … // use `await` freely })); 
like image 68
Bergi Avatar answered Sep 28 '22 23:09

Bergi