Storing kernel response in a variable

Hi,

I’m trying to build a custom completer extension following the completer example. The list of custom suggestions would have a static list of tokens + a dynamic list of tokens that needs to be fetched by running a Python code in the kernel. I tried to integrate this code in kernel-messaging example.

This is a snippet of my code additions:

some_list = []
function modifyList(model: KernelModel): void {
    var data = model.output["data"];
    // some processing on data 
   // append processed data to some_list
   console.log(some_list); //this outputs the modified list correctly
....
....
export function completionHint(
    editor: CodeEditor.IEditor,
    sessionContext: ISessionContext,
    model: KernelModel
  ): CompletionHandler.IReply {
.....
const code = 
`
some Python code
`
model.execute(code);
model.stateChanged.connect((__getResults) => {
      modifyList(model)
    });

console.log(some_list) // this doesn't contain the appended tokens

I understand that executeRequest issues a lumino signal on completion of the kernel messaging. The modifyList correctly logs the processed data items in the console, however the list modification is not available until all the synchronous code has run. I have tried adding await here but it’s not helping either. Most of the example I see are connecting the kernel output to some sort of widget like text area or output window. I have also checked [javascript - How to use events in jupyterlab extensions? - Stack Overflow](this link). This is also just logging the messages in the console on stateChanged event, and not updating any local variables.

Please provide some directions.