public JavaScriptValue CreatePromise(Task <JavaScriptValue> task)
        {
            lock (taskSync)
            {
                outstandingItems++;
            }

            JavaScriptValue resolve;
            JavaScriptValue reject;
            JavaScriptValue promise = JavaScriptValue.CreatePromise(out resolve, out reject);

            reject.AddRef();
            resolve.AddRef();
            task.ContinueWith(
                (antecedent, state) =>
            {
                switch (antecedent.Status)
                {
                case TaskStatus.Canceled:
                case TaskStatus.Faulted:
                    reject.CallFunction(
                        JavaScriptValue.GlobalObject,
                        JavaScriptValue.CreateError(JavaScriptValue.FromString(antecedent.Exception.Message)));
                    break;

                case TaskStatus.RanToCompletion:
                    var result = antecedent.GetAwaiter().GetResult();
                    resolve.CallFunction(JavaScriptValue.GlobalObject, result);
                    break;
                }

                lock (taskSync)
                {
                    outstandingItems--;
                }
                reject.Release();
                resolve.Release();
            },
                null,
                this.taskScheduler);
            return(promise);
        }