dfeprado / TPromise

O conceito PROMISE do ES no DELPHI
GNU General Public License v3.0
16 stars 8 forks source link

TPromise

The JS Promise concept on Delphi!

v2.0.0

News

Important: Version 2 introduces break changes. If you're already using TPromise v1, you should do some changes to your code. If you do not want to use v2, keep using v1 by setting the head to tag 1.1.0. See Break changes introduced by V2 to see what needs to change from v1 to v2.

Usage

All units are inside src/ folder.

uPromiseInterface defines IPromise interface and anonymous functions. You're supposed to use it more than the another unit.

The another unit is uPromise, that defines de TPromise class that implements IPromise interface.

The golden usage rule is:

Example:

unit uBrowserFetch;
// This units creates and returns a IPromise. So, it uses uPromiseInterface and uPromise units.

interface

uses uPromiseInterface;
// Implements a browser fetch() like function
function fetch(const url: string): IPromise<String, String>;

implementation

uses uPromise;
function fetch(const url: string): IPromise<String, String>;
begin
    result := TPromise.New(
        procedure (resolve: TOnResolveCallback<String>; reject: TOnRejectCallback<String>; const utils: TPromiseUtils)
        begin
            // Do some network stuff
            resolve(fetchResultStr);
        end
    );
end;
unit uMainForm;

// This unit just need a reference to IPromise<T, E>, and in implementation. So it just use uPromiseInterface

interface
    ...

implementation

uses uPromiseInterface, uBrowserFetch;

...

procedure TMainForm.SomeButtonClick(Sender: TObject);
begin
    fetch('https://my.login.page.com/?user=foo&pwd=bar')
        .&then(
            procedure (const result: string)
            begin
                // Do something with fetch result
            end
        );
end;

...

Examples

See the samples/ folder. There's a network fetch sample using TPromise.

Break changes introduced by v2

Renamed codes

New features