Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added JsCreatePromise to JsRT #2581

Closed
wants to merge 1 commit into from

Conversation

kfarnung
Copy link
Contributor

No description provided.

@liminzhu
Copy link
Collaborator

+@curtisman , this is the context #2541


return JsNoError;
});
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: indentation level in the rest of this file is 4 spaces.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch, VS doesn't seem to auto-detect indentation.

Copy link
Contributor

@boingoing boingoing left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, should confirm we want the JsCreatePromise API to be a common export.

/// Requires an active script context.
/// </para>
/// </remarks>
/// <param name="promise">The new Promise object.</param>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just a nitpick, this param tag is all one line while the others are split up.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We're inconsistent about this elsewhere in this file as well. I wonder if it has to do with line length and VS doing something automatically with formatting?

@@ -2866,6 +2867,32 @@ CHAKRA_API JsSetPromiseContinuationCallback(_In_ JsPromiseContinuationCallback p
/*allowInObjectBeforeCollectCallback*/true);
}

CHAKRA_API JsCreatePromise(_Out_ JsValueRef *promise, _Out_ JsValueRef *resolve, _Out_ JsValueRef *reject)
{
return ContextAPIWrapper<true>([&](Js::ScriptContext *scriptContext, TTDRecorder& _actionEntryPopper) -> JsErrorCode {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nitpick, indentation here.

@liminzhu
Copy link
Collaborator

liminzhu commented Feb 23, 2017

I'm a little confused about what resolveFunction and rejectFunction output are and how I would use them. Would appreciate a short snippet (we need to document the API anyways) 😄 .

Let's say JsCreatePromise is equivalent to new Promise((res, rej) => {...}), would this make more sense?

JsCreatePromise(_In_ JsValueRef executor, _Out_ JsValueRef *promise)

executor could be a native function created by JsCreateFunction.

@boingoing
Copy link
Contributor

@liminzhu

I'm a little confused about what resolveFunction and rejectFunction output are and how I would use them.

These are essentially the same functions passed to the executor. Another method to fetch them might be like this:

var resolve, reject;
function executor(res, rej) { resolve = res; reject = rej; };
var promise = new Promise(executor);

Goal would be to allow our native code to resolve the promise whenever it wants to. One use might be an API which reads a file from the network and returns a promise resolved with the contents of that file. Native code can construct the promise using JsCreatePromise and hold-on to resolveFunction for that promise. Assume the native-side performs some processing to fetch the result data and when that is done it can call the resolveFunction with that result data which will handle resolving the promise in the engine. I guess the key thing here is that the native code can resolve the promise in an async way without having to create an executor function which would manually suck out the resolve/reject functions.

@liminzhu
Copy link
Collaborator

liminzhu commented Feb 23, 2017

Thanks @boingoing ! Makes much sense now. resolveFunction and rejectFunction are hooks which you can JsCallFucntion upon to resolve/reject a promise natively. I like it and we'd need some proper documentation explaining how this would work. Please move the API to ChakraCore.h to avoid getting shipped to ChakraFull atm.

PARAM_NOT_NULL(resolve);
PARAM_NOT_NULL(reject);

*promise = nullptr;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is this needed? (same applies to two below)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is not mandatory to have either resolve() or reject() methods FYI.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@obastemur I was just following the pattern of other methods. The idea was to ensure that regardless of how the flow changed we would always have set some value. I realize that in this flow we will always set a value, so I'm fine removing it if others agree.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see a reason to remove this. In this case the compiler will be able to figure out that this operation is not needed -- if the flow ever changes, we are protected.

@galvesribeiro
Copy link

LGTM. I just would like to mention that it is not required to have both reject() and response() methods. And if possible, it would be good to accept multiple methods accepted because people can catch multiple types of errors on diff methods. For example:

mypromise.then(...).then(...).catch(...).catch(...) <- that is totally viable.

@kfarnung
Copy link
Contributor Author

@galvesribeiro I think you might have it backwards, the resolve and reject parameters are out parameters that will contain pointers to the functions that the native code will call when it has either resolved or rejected the promise. These are equivalent to the parameters that come into the executor method when creating a promise in JavaScript.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise

For continuations you would just use the JsRT functions to get the property (then, catch, etc.) and then call it normally with JsCallFunction.

In C#:

JavaScriptPropertyId thenId = JavaScriptPropertyId.FromString("then");
JavaScriptValue thenFunction = promise.GetProperty(thenId);

JavaScriptValue resolveFunc = JavaScriptValue.CreateFunction(ResolveCallback);
JavaScriptValue rejectFunc = JavaScriptValue.CreateFunction(RejectCallback);
JavaScriptValue thenPromise = thenFunction.CallFunction(promise, resolveFunc, rejectFunc);

Since it's just a JavaScript function, you can choose to leave out the second parameter when calling it.

@galvesribeiro
Copy link

Oh! Right, I understood it completely wrong, sorry. You are right! Good work!

CHAKRA_API JsCreatePromise(_Out_ JsValueRef *promise, _Out_ JsValueRef *resolve, _Out_ JsValueRef *reject)
{
return ContextAPIWrapper<true>([&](Js::ScriptContext *scriptContext, TTDRecorder& _actionEntryPopper) -> JsErrorCode {
PERFORM_JSRT_TTD_RECORD_ACTION_NOT_IMPLEMENTED(scriptContext);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need to implement this for TTD? @mrkmarron

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I spoke with @mrkmarron and this was his recommendation. If the user is running TTD and runs into this we'll fail fast and deal with it at that time.

@kfarnung kfarnung changed the base branch from master to release/2.0 February 24, 2017 19:00
@kfarnung
Copy link
Contributor Author

Closing this one and retargeting to release/2.0 in #2594.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

9 participants