Skip to content

Instantly share code, notes, and snippets.

@tomekzaw
Created January 13, 2022 06:58
Show Gist options
  • Save tomekzaw/83167d8addedb75263459f97cce17549 to your computer and use it in GitHub Desktop.
Save tomekzaw/83167d8addedb75263459f97cce17549 to your computer and use it in GitHub Desktop.
swmansion::jsiutils::Promise
using namespace swmansion::jsiutils::Promise;
auto promise = Promise::createFromHostFunction(
rt,
[](jsi::Runtime &rt, Promise::Resolve resolve, Promise::Reject reject) {
resolve(jsi::Value(42));
});
#include <jsi/jsi.h>
#include "Promise.h"
using namespace facebook;
namespace swmansion::jsiutils::Promise {
jsi::Value createFromHostFunction(jsi::Runtime &rt, Executor &&executor) {
return rt.global()
.getPropertyAsFunction(rt, "Promise")
.callAsConstructor(
rt,
jsi::Function::createFromHostFunction(
rt, jsi::PropNameID::forAscii(rt, "executor"), 2,
[executor{std::move(executor)}](
jsi::Runtime &rt, const jsi::Value &thisValue,
const jsi::Value *arguments, size_t count) -> jsi::Value {
auto resolve = std::make_shared<jsi::Value>(rt, arguments[0]);
auto reject = std::make_shared<jsi::Value>(rt, arguments[1]);
executor(
rt,
[&rt, resolve](jsi::Value &&value) -> void {
resolve->asObject(rt).asFunction(rt).call(rt, value);
},
[&rt, reject](jsi::Value &&value) -> void {
reject->asObject(rt).asFunction(rt).call(rt, value);
});
return {};
}));
}
} // namespace swmansion::jsiutils::Promise
#pragma once
#include <jsi/jsi.h>
using namespace facebook;
namespace swmansion::jsiutils::Promise {
using Resolve = std::function<void(jsi::Value &&)> &&;
using Reject = std::function<void(jsi::Value &&)> &&;
using Executor =
std::function<void(jsi::Runtime &, Resolve &&resolve, Reject &&reject)> &&;
jsi::Value createFromHostFunction(jsi::Runtime &rt, Executor &&executor);
} // namespace swmansion::jsiutils::Promise
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment