Quickjs -- Running code on Promise completion or rejection

379 views Asked by At

I am using quickjs js engine. As a part of my requirments, I wanted to create a Promise and do certain actions when its resolved or rejected. (Basically do set then and catch functions). I know promise is created as such:

JSValue resolvingFuncs[2];
JSValue promise = JS_NewPromiseCapability(ctx,resolvingFuncs);

The two resolving functions be the resolve and reject. But how do I set then and catch functions ?

1

There are 1 answers

1
cboy On

Just save resolve and reject in C, and call resolve or reject function using JS_Call function.

Like this:

JSValue ret = JS_Call(data->sp_ctx->js_ctx, data->resolve, data->promise, 1, &file_handle_value);
if(JS_IsError(data->sp_ctx->js_ctx, ret) || JS_IsException(ret)) {
    js_std_dump_error(data->sp_ctx->js_ctx);
}