AmyrAhmady / samp-node

a SA-MP plugin to run nodejs scripts
71 stars 8 forks source link

promise resolve/reject triggers no effect in callback #27

Closed dockfries closed 11 months ago

dockfries commented 1 year ago

see here

I tried to support promise for dialog's show method and ran into problems implementing it.

It's not just in this one callback, but also in other callbacks, such as OnPlayerDisconnect, which internally calls resolve or reject a promise at one point.

It must be wrapped in a layer of setTimeout or setInterval to work, and there is a minimal probability that setImmediate or process.nextTick will work, and direct calls will not take effect at all.

node version: 16.17.1 samp-node version: 2.1.0

OnDialogResponse(
  (
    playerid: number,
    dialogid: number,
    response: number,
    listitem: number,
    inputbuf: number[]
  ): number => {
    const callback = Dialog.waitingQueue.get(playerid);
    if (!callback) return 0;
    // bug: does not trigger resolve of promise
    // fix: it only works if you put it in an event loop
    setTimeout(() => callback.resolve({ response, listitem, inputbuf }));
    return 1;
  }
);