gpt4 book ai didi

javascript - ES8 立即调用异步函数表达式

转载 作者:可可西里 更新时间:2023-11-01 01:47:08 26 4
gpt4 key购买 nike

我没有看到这些构造被广泛使用,但我发现自己编写它们是为了在通常不会返回 promise 的函数中使用 async/await,例如

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);
}
})();
});

相对于

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);
}
});

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);
});
}
});
});

这是“一件事”吗?这里有我应该注意的陷阱吗?在这种情况下使用 async/await 有什么缺点?

最佳答案

Is this 'a thing'?

是的。它不时出现,例如here .它们被称为 IIAFEs :-)
如果你想把焦点放在箭头上,你也可以称它们为 IIAAFs。

Are there pitfalls here I should be aware of?

每当您调用一个返回 promise 的函数并且不将结果返回到其他地方时,您自己要对 promise 负责——这意味着您必须处理它的错误。所以模式通常应该看起来像

(async () => {

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

如果您不想让自己担心未处理的拒绝事件。

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

then 版本相比,并不多。但是,您说“外部库不期望此回调返回值”,这可能暗示该库与异步回调不兼容,因此请注意您在什么时候做什么。它还可能取决于从回调中同步抛出的异常,因此这完全取决于库在这里的期望(如果没有期望,将来是否会改变)。您不希望将来出现不兼容性,以防库开始特殊处理 promise 返回值。

不过,我还是会推荐第二种模式,直接将async函数作为回调直接传递,因为它的可读性更好。如果您想避免向库返回 promise ,请创建一个包装回调的辅助函数:

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

你可以这样使用:

chan.consume(queue, toVoid(async (msg) => {
… // use `await` freely
}));

关于javascript - ES8 立即调用异步函数表达式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40745153/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com