Blog

All Blog Posts  |  Next Post  |  Previous Post

Delphi Promises Now in TMS FNC Cloud Pack

Today

TMS Software Delphi  Components
If you've worked with cloud APIs, you know the struggle—waiting for responses, handling event callbacks, and making sure each request happens in the right order. Things can quickly get messy, especially when multiple actions depend on each other.

To improve this, we introduced Promises in TMS FNC Core a while ago. Now, we're excited to bring Promise support directly into TMS FNC Cloud Pack! Instead of managing event handlers, Promises let you write clear, sequential code that’s easier to read and maintain.

What’s the Problem with Events?

Event-driven programming works, but it can lead to scattered logic. Take this example, where we fetch a folder list:
procedure TForm1.btnClick(Sender: TObject);
begin
  TMSFNCCloudStorageServices1.GetFolderList;
end;

procedure TForm1.TMSFNCCloudStorageServices1GetFolderList(Sender: TObject;
  const AItems: TTMSFNCCloudItems;
  const ARequestResult: TTMSFNCCloudBaseRequestResult);
begin
  //Handle AItems result here
end;
Most often these 2 pieces of code will be far apart, which is harder to oversee. On top of that, the OnGetFolderList event will trigger each time GetFolderList is called, making it almost impossible to implement custom logic for separate GetFolderList calls.

How Promises Make It Simpler

With Promises, you chain operations together, keeping everything in one place:
procedure TForm1.GetFolderList(AFolder: TTMSFNCCloudItem = nil);
begin
  TMSFNCCloudStorageServices1.GetFolderListPromise(AFolder)
  .Main.ThenBy(procedure (const AVoid: TVoid)
  begin
    //Handle TMSFNCCloudStorageServices1.PromiseFolderList result here
  end);
end;
Now the logic is clear: Get the folder list, then update the UI—all in one place, without scattered event handlers.

A Real-Life Example: Access Token Renewal

When integrating Promises, we carefully evaluate where they bring the most value. One key area we identified is access token renewal. Tokens with shorter lifetimes can expire while you're working on something, causing requests to fail unexpectedly. Without Promises, handling renewals requires additional checks, event handlers, and retry logic.

To solve this, we introduced FetchAccessToken in TTMSFNCCloudOAuth. This means all descendant components automatically benefit from this improvement!

Calling FetchAccessToken returns a string with the latest valid access token. If the token is expired, it automatically retrieves a new one using the refresh token—all in a single call! Usage is as simple as:
procedure TForm1.Button1Click(Sender: TObject);
begin
  TMSFNCCloudGoogleCalendar1.FetchAccessToken.ThenBy(procedure (const AValue: string)
  begin
    TMSFNCCloudGoogleCalendar1.GetCalendar('calendarID');
  end);
end;
Or if you prefer Await:
procedure TForm1.Button1Click(Sender: TObject);
begin
  TThread.CreateAnonymousThread(
  procedure
  begin
    TMSFNCCloudGoogleCalendar1.FetchAccessToken.Await;
    TMSFNCCloudGoogleCalendar1.GetCalendar('calendarID');
  end).Start;
end;

Try It Today!

As a first step, we integrated promises into a selection of most-used services. These are:
  • TTMSFNCCloudStorageServices, including separate improvements for:
    • TTMSFNCCloudGoogleDrive
    • TTMSFNCCloudDropBox
    • TTMSFNCCloudMicrosoftOneDrive
    • TTMSFNCCloudBox
  • TTMSFNCCloudGoogleGmail
  • TTMSFNCCloudMicrosoftOutlookMail
  • TTMSFNCCloudStellarDataStore
  • TTMSFNCCloudOAuth (FetchAccessToken)

Update to the latest version and take your cloud-powered applications to the next level! Have any questions, remarks or need further assistance? Feel free to reach out and don't forget to check our feature request page if you need Promises support for other cloud services.


Tunde Keller




This blog post has not received any comments yet.



Add a new comment

You will receive a confirmation mail with a link to validate your comment, please use a valid email address.
All fields are required.



All Blog Posts  |  Next Post  |  Previous Post