[Pas2js] Promises stripper project

warleyalex warleyalex at yahoo.com.br
Fri Feb 2 13:50:01 CET 2018


While converting a Promises project to Pas2JS,

a) hum, there's a minor typo in the Web.pas
    //function getElementsyClassName(aNames : string) : TJSHTMLCollection;

b) Since Pas2JS does not support anonymous methods yet, it's so boring
writing sub functions :( to do simple things, for instance:

     setTimeout(function () {
         console.log('1')
         setTimeout(function () {
             console.log('2')
         }, 1000)
     }, 1000)

c) the TJSPromise (web.pas) is incomplete, I've created my wrapper around
the Promises.

*See the Promise project live preview:*
https://rawcdn.githack.com/pas2js/master/master/projPromises/projPromises.html

//-------------------------------------------------
unit uPromises;

{$MODE objfpc}
{$MODESWITCH externalclass}

interface

  uses JS, Web;

type
  TJSValueDynArray = array of JSValue;
  JDeferred = class;
  TJPromiseCallback = procedure(Value: JSValue);
  TJDeferredObject_fn = function(d: TJPromiseCallback): JSValue;
  TJDeferredObject = procedure(resolve: TJPromiseCallback; reject:
TJPromiseCallback);
  TJPromiseCallback_fn = function(Value: JSValue): JSValue;
  TJDeferredEventHandler = function(event: JSValue): JSValue;

  JPromise = class external name 'Promise'
    constructor new(fn: TJDeferredObject_fn { = nil}); overload;
    constructor new(resolve: TJDeferredObject_fn; reject:
TJDeferredObject_fn); overload;
    constructor new(fn: TJDeferredObject); overload;
    function always(alwaysCallbacks: TJSValueDynArray): JPromise;
    function done(doneCallbacks: TJSValueDynArray): JPromise; overload;
    function done(doneCallbacks: JSValue): JPromise; overload;
    function fail(failCallbacks: TJSValueDynArray): JPromise;
    function progress(progressCallbacks: TJSValueDynArray): JPromise;
    function state(): string;
    function &then(doneCallbacks: JSValue; failCallbacks: JSValue{ =
undefined};
      progressCallbacks: JSValue { = undefined}): JPromise; external name
'then';
    function &then(onFulfilled: TJPromiseCallback_fn = nil): JPromise;
overload;
      external name 'then';
    function &then(onFulfilled: TJPromiseCallback_fn; onRejected:
      TJPromiseCallback_fn): JPromise; overload; external name 'then';
    function &then(onFulfilled: TJPromiseCallback; onRejected:
      TJPromiseCallback): JPromise; overload; external name 'then';
    function catch(rejecTJPromiseCallback: JSValue = nil): JPromise;
overload;
    function catch(rejecTJPromiseCallback: TJPromiseCallback_fn): JPromise;
overload;
    class function promise(target: JSValue): JPromise;
  end;

type
  JDeferred = class external name 'Promise'(JPromise)
    function notify(args: TJSValueDynArray): JDeferred;
    function notifyWith(context: JSValue; args: TJSValueDynArray):
JDeferred;
    function reject(args: TJSValueDynArray): JDeferred; overload;
    function reject(args: JSValue): JDeferred; overload;
    function reject(args: TJDeferredEventHandler): JDeferred; overload;
    function rejectWith(context: JSValue; args: TJSValueDynArray):
JDeferred;
    function resolve(args: TJSValueDynArray): JDeferred; overload;
    function resolve(value: JSValue = nil): JPromise; overload;
    function resolveWith(context: JSValue; args: TJSValueDynArray):
JDeferred;
    function all(iterable: TJSValueDynArray): JPromise; overload;
    function all(iterable: TJSArray): JPromise; overload;
    function race(iterable: TJSValueDynArray): JPromise;
  end;

  { global external functions }
var Promise: JDeferred; external name 'Promise';// property;
function Error(message: JSValue): JSValue; external name 'Error';

implementation

end.
//-------------------------------------------------

I don't know if is worthy to strip down the Web.pas and JS.pas units and
organize in separate units, such
ECMA.Promises.pas
ECMA.JSON.pas
ECMA.Date.pas
ECMA.Math.pas




--
Sent from: http://pas2js.38893.n8.nabble.com/


More information about the Pas2js mailing list