As the other answer said, you can't put await
in a lambda
. But if await
is the outermost operator in the expression, you can omit both the async and await and have the lambda directly return the awaitable:
self.voice_channel.play(discord.FFmpegPCMAudio('music.mp3'),
after=lambda e: self.test(x))
Without the await
, the lambda simply returns self.test(x)
. As that value was to be awaited, self.test(x)
must obviously return an awaitable object. This awaitable object will be received by code that at some point invokes o.after(e)
and awaits the result. In other words, await o.after(e)
will be equivalent to await self.test(x)
, which is what you wanted to accomplish.
In general, an async def
function:
async def foo():
return await bar()
can be replaced with:
def foo():
return bar()
Although foo()
is not async in the latter case, the end result is the same: await foo()
will return the result of awaiting bar()
. See this answer for another example.