node-fswin icon indicating copy to clipboard operation
node-fswin copied to clipboard

Promise-based API support

Open felipecrs opened this issue 2 months ago • 6 comments

Currently, fswin only provides callback-based APIs. It would be great to have native Promise support to improve compatibility with modern async/await code.

Current usage:

import { promisify } from "node:util";
import fswin from "fswin";

function getAttributesWrapper(
  path: string,
  callback: (argument0: Error, argument1: fswin.Attributes) => void,
): void {
  fswin.getAttributes(path, function (succeeded) {
    succeeded
      ? callback(undefined, succeeded)
      : callback(new Error(`Failed to set attributes of ${path}`), undefined);
  });
}

const getAttributesAsync = promisify(getAttributesWrapper);

const attributes = await getAttributesAsync(file);

Instead of something like:

import fswin from "fswin";

const attributes = await fswin.getAttributesAsync(file);

What do you think?

felipecrs avatar Nov 06 '25 14:11 felipecrs

I think we can implant promise api but we may use Promise() instead of util.promisify()

fswin.promises = {
  getAttributes: () => new Promise(resolve => fswin.getAttributes(resolve))
};

const attributes = await fswin.promises.getAttributes(file);
if (!attributes) {
  // you may consider this as error
}

xxoo avatar Nov 06 '25 20:11 xxoo

fswin.promises seems neat!

But in your specific example, I believe you should throw an error.

felipecrs avatar Nov 06 '25 20:11 felipecrs

I think we have to keep the promise api return the same value as sync version. You may throw an error when attributes == null if that is necessary.

xxoo avatar Nov 08 '25 02:11 xxoo

New apis are available now. I chose the "Async" suffix instead of creating a new group for easier documentation.

xxoo avatar Nov 09 '25 10:11 xxoo

You may throw an error when attributes == null if that is necessary.

Then I think you should change the return type from Promise<fswin.Attributes> to Promise<fswin.Attributes | null>. 😉

felipecrs avatar Nov 10 '25 14:11 felipecrs

By the way, I tested the new Async methods. They work great! Thanks a lot!

  • Ref https://github.com/felipecrs/clipboard-sync/commit/c746d0cbc1c9e5c7c574307b2fa96bfeb83cb730

felipecrs avatar Nov 10 '25 14:11 felipecrs