JavaScript等待承诺

时间:2020-07-10 15:09:47

标签: javascript asynchronous async-await resolve

我需要# LCUI.py import ctypes NULL = 0 dll = ctypes.CDLL('lcui.dll') class LCUI_WidgetRec_ (ctypes.Structure): pass LCUI_Widget =ctypes.POINTER(LCUI_WidgetRec_) LCUI_Init = dll.LCUI_Init LCUIWidget_GetRoot = dll.LCUIWidget_GetRoot LCUIWidget_GetRoot.restype = LCUI_Widget LCUIBuilder_LoadFile = dll.LCUIBuilder_LoadFile LCUIBuilder_LoadFile.restype = LCUI_Widget Widget_Append = dll.Widget_Append Widget_Append.argtypes = LCUI_Widget, LCUI_Widget Widget_Append.restype = ctypes.c_int Widget_Unwrap = dll.Widget_Unwrap LCUIWidget_GetById = dll.LCUIWidget_GetById LCUIWidget_GetById.restype = LCUI_Widget class LCUI_WidgetEventRec_ (ctypes.Structure): pass LCUI_WidgetEvent = ctypes.POINTER(LCUI_WidgetEventRec_) LCUI_WidgetEventFunc = ctypes.CFUNCTYPE(ctypes.c_void_p, LCUI_Widget, LCUI_WidgetEvent, ctypes.c_void_p) _Widget_BindEvent = dll.Widget_BindEvent _Widget_BindEvent.argtypes = LCUI_Widget, ctypes.c_char_p, LCUI_WidgetEventFunc, ctypes.c_void_p, ctypes.c_void_p def Widget_BindEvent(widget, event_name, func, data=None, destroy_data=None): event_name = event_name.encode('utf8') func = LCUI_WidgetEventFunc(func) data = ctypes.cast(data, ctypes.c_void_p), destroy_data = ctypes.cast(destroy_data, ctypes.c_void_p) return _Widget_BindEvent( widget, event_name, func, data, destroy_data ) LCUI_Main = dll.LCUI_Main 等待(通过外部过程)定义someVariable。函数fetchAndInstantiate检测何时定义了该变量。

我有这两个功能:

getResult

函数async function getResult(){ if(typeof someVariable !== 'undefined'){ console.log('yup'); Promise.resolved(someVariable); }else{ console.log('nop'); setTimeout(getResult, 250); } } fetchAndInstantiate = async function(a, b, c){ delete someVariable; console.log('called fetchAndInstantiate'); document.dispatchEvent(new CustomEvent('wasmcall', {detail: { wasm_mod: a, function: b, args: c}})); await getResult().then(function(result){return result;}); } 调用函数fetchAndInstantiate,我希望它等待到诺言解决。但是,getResult被调用时getResult返回未定义的承诺。

如何使setTimeout仅在定义someVariable时才能解决?

2 个答案:

答案 0 :(得分:0)

您可以创建一个异步延迟:

Node* createTree(int parent[], int n) {
  Node** nodes = new Node*[n];
  for ( int i = 0; i < n; i++ )
      nodes[i] = new Node(i);
  int rootIndex = 0;
  for ( int i  = 0; i < n; i++ ) {
    if ( parent[i] == -1 ) {
      rootIndex = i;
    } else {
      if ( nodes[parent[i]] -> left == NULL ) {
        nodes[parent[i]] -> left = nodes[i];
      } else if ( nodes[parent[i]] -> right == NULL ) {
          nodes[parent[i]] -> right = nodes[i];
      }
    }
    
  }
  return nodes[rootIndex];
}

并在您的异步函数中使用它:

const delay = milliseconds => new Promise(resolve, setTimeout(resolve, milliseconds));

这将继续等待,直到定义了async function getResultAsync() { while (true) { if (typeof someVariable !== 'undefined') { return someVariable; } await delay(250); } } 为止,每次检查之间有250毫秒的延迟,而不会阻塞。

请注意,您必须在异步函数内返回某些内容;这样可以解决返回的someVariable

然后在您的Promise函数中进行消费:

fetchAndInstantiate

您不需要将fetchAndInstantiate = async function(a, b, c) { delete someVariable; console.log('called fetchAndInstantiate'); document.dispatchEvent(new CustomEvent('wasmcall', {detail: { wasm_mod: a, function: b, args: c}})); await getResultAsync(); } await混合使用; then()为您取消await分辨率。

答案 1 :(得分:0)

您必须确保getResult返回预期的承诺。

此外,使用setInterval可以完成您想要的操作,而无需弄乱代码。

async function getResult(){
    var result = await new Promise(function(resolve) {
    const interval = setInterval(function(){
      if(typeof someVariable !== 'undefined'){
        clearInterval(interval);
        resolve(someVariable);
      }
    }, 250);
  });
  return result;
}

fetchAndInstantiate = async function(a, b, c){
    delete someVariable;
    console.log('called fetchAndInstantiate');
    document.dispatchEvent(new CustomEvent('wasmcall', {detail: { wasm_mod: a, function: b, args: c}}));
    var result = await getResult();
    return result;
}