Skip to content
This repository has been archived by the owner on Oct 21, 2023. It is now read-only.

Sessions with async functions does not works #27

Open
nightSamurai opened this issue Nov 20, 2020 · 1 comment
Open

Sessions with async functions does not works #27

nightSamurai opened this issue Nov 20, 2020 · 1 comment

Comments

@nightSamurai
Copy link

Hi everyone!

I uses Telegraf + Redis-session + Stage in my bot. But session doesn't works with Promise

This very similar like this issues ( 1, 2, 3, 4 ), but nothing was helpful for me

Into my scene on "enter" event i try to set something like this: ctx.scene.state.{key} = {value}. And this work fine only if use it without promise or async function. For example:

export const scene = new BaseScene(MY_SCENE)

// My enter scene logic
scene.enter(async (ctx) => {
    // this works
    ctx.scene.state.number = 1 
    
    // this set value into a state variable, but NOT save it in sessions
    ctx.scene.state.promise = await (
        new Promise<number>((resolve, reject) => {
            setTimeout(() => {
                resolve(1)
            }, 1000)
        })
    )

    console.log( ctx.scene.state )
    // output { number: 1, promise: 1 }


    // another code here...
})

// on any of event type will be the same behavior
scene.on('message', (ctx) => {
    console.log(
        ctx.scene.state
    )
    // output { number: 1 }
    // expected to be { number: 1, promise: 1 }
})

So, maybe exists the way, to trigger sync process? Or some utility function for force update the session?

I will be glad of any help, thanks!

@syazsolo
Copy link

syazsolo commented Sep 22, 2023

I know its been a long time, but I had similar problem, and the solution is to
await ctx.scene.enter(MY_SCENE)
instead of ctx.scene.enter(MY_SCENE) without await

hope this helps

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants