Skip to content

Commit

Permalink
Merge pull request #113 from vim-denops/fix-request_async
Browse files Browse the repository at this point in the history
🐛 Fix behavior of `request_async()`
  • Loading branch information
lambdalisue committed Sep 11, 2021
2 parents e386441 + 98dad75 commit a1e9ea0
Showing 1 changed file with 34 additions and 4 deletions.
38 changes: 34 additions & 4 deletions denops/@denops-private/host/invoker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,23 @@ export class Invoker {
failure: string, // Callback ID
): Promise<void> {
this.#service.dispatch(name, fn, args)
.then((r) => this.#service.call("denops#callback#call", success, r))
.catch((e) => this.#service.call("denops#callback#call", failure, e))
.catch((e) => {
console.error(`${e.stack ?? e.toString()}`);
.then(async (r) => {
try {
await this.#service.call("denops#callback#call", success, r);
} catch (e) {
console.error(`${e.stack ?? e.toString()}`);
}
})
.catch(async (e) => {
try {
await this.#service.call(
"denops#callback#call",
failure,
toErrorObject(e),
);
} catch (e) {
console.error(`${e.stack ?? e.toString()}`);
}
});
return Promise.resolve();
}
Expand All @@ -52,3 +65,20 @@ export class Invoker {
export function isInvokerMethod(value: string): value is keyof Invoker {
return value in Invoker.prototype;
}

// https://github.com/vim-denops/denops.vim/issues/112
function toErrorObject(
err: unknown,
): { name: string; message: string; stack?: string } {
if (err instanceof Error) {
return {
name: err.name,
message: err.message,
stack: err.stack,
};
}
return {
name: typeof err,
message: `${err}`,
};
}

0 comments on commit a1e9ea0

Please sign in to comment.