diff --git a/NiL.JS/BaseLibrary/Promise.cs b/NiL.JS/BaseLibrary/Promise.cs index 8167b6b11..eefac234d 100644 --- a/NiL.JS/BaseLibrary/Promise.cs +++ b/NiL.JS/BaseLibrary/Promise.cs @@ -11,14 +11,7 @@ namespace NiL.JS.BaseLibrary { - public enum PromiseState - { - Pending, - Fulfilled, - Rejected - } - - public sealed class Promise + public sealed class Promise : IPromiseLike { private Task _innerTask; private Function _callback; @@ -177,12 +170,12 @@ private void callbackInvoke() _outerTask.SetResult(JSValue.undefined); } - public static Promise resolve(JSValue data) + public static IPromiseLike resolve(JSValue data) { return new Promise(fromResult(data)); } - public static Promise race(IIterable promises) + public static IPromiseLike race(IIterable promises) { if (promises == null) return new Promise(fromException(new JSException(new TypeError("Invalid argruments for Promise.race(...)")))); @@ -190,7 +183,7 @@ public static Promise race(IIterable promises) return new Promise(whenAny(promises.AsEnumerable().Select(convertToTask).ToArray())); } - public static Promise all(IIterable promises) + public static IPromiseLike all(IIterable promises) { if (promises == null) return new Promise(fromException(new JSException(new TypeError("Invalid argruments for Promise.all(...)")))); @@ -203,12 +196,12 @@ private static Task convertToTask(JSValue arg) return (arg.Value as Promise)?.Task ?? fromResult(arg); } - public Promise @catch(Function onRejection) + public IPromiseLike @catch(Function onRejection) { return then(null, onRejection); } - public Promise then(Function onFulfilment, Function onRejection) + public IPromiseLike then(Function onFulfilment, Function onRejection) { return then( onFulfilment == null ? null as Func : value => onFulfilment.Call(JSValue.undefined, new Arguments { value }), @@ -216,7 +209,7 @@ public Promise then(Function onFulfilment, Function onRejection) } [Hidden] - public Promise then(Func onFulfilment, Func onRejection) + public IPromiseLike then(Func onFulfilment, Func onRejection) { if (onFulfilment == null && onRejection == null) return resolve(JSValue.undefined); diff --git a/NiL.JS/Core/Functions/AsyncFunction.cs b/NiL.JS/Core/Functions/AsyncFunction.cs index 9877811ad..1a53ffccd 100644 --- a/NiL.JS/Core/Functions/AsyncFunction.cs +++ b/NiL.JS/Core/Functions/AsyncFunction.cs @@ -29,7 +29,7 @@ public void Build(JSValue promise) private JSValue subscribeOrReturnValue(JSValue promiseOrValue) { - var p = promiseOrValue?.Value as Promise; + var p = promiseOrValue?.Value as IPromiseLike; if (p == null) return promiseOrValue; diff --git a/NiL.JS/Core/IPromiseLike.cs b/NiL.JS/Core/IPromiseLike.cs new file mode 100644 index 000000000..c939cb2c7 --- /dev/null +++ b/NiL.JS/Core/IPromiseLike.cs @@ -0,0 +1,30 @@ +using System; +using NiL.JS.BaseLibrary; +using NiL.JS.Core.Interop; + +namespace NiL.JS.Core +{ + public enum PromiseState + { + Pending, + Fulfilled, + Rejected + } + + public interface IPromiseLike + { + [Hidden] + PromiseState State { get; } + +#pragma warning disable IDE1006 + + IPromiseLike @catch(Function onRejection); + + IPromiseLike then(Function onFulfilment, Function onRejection); + + [Hidden] + IPromiseLike then(Func onFulfilment, Func onRejection); + +#pragma warning restore IDE1006 + } +} diff --git a/NiL.JS/Expressions/AwaitExpression.cs b/NiL.JS/Expressions/AwaitExpression.cs index 14050cf8d..3012fa837 100644 --- a/NiL.JS/Expressions/AwaitExpression.cs +++ b/NiL.JS/Expressions/AwaitExpression.cs @@ -46,7 +46,7 @@ public override JSValue Evaluate(Context context) return null; } - if (result != null && (result._valueType < JSValueType.Object || !(result.Value is Promise))) + if (result != null && (result._valueType < JSValueType.Object || !(result.Value is IPromiseLike))) return result; context._executionMode = ExecutionMode.Suspend;