Promise-based API support
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?
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
}
fswin.promises seems neat!
But in your specific example, I believe you should throw an error.
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.
New apis are available now. I chose the "Async" suffix instead of creating a new group for easier documentation.
You may throw an error when
attributes == nullif that is necessary.
Then I think you should change the return type from Promise<fswin.Attributes> to Promise<fswin.Attributes | null>. 😉
By the way, I tested the new Async methods. They work great! Thanks a lot!
- Ref https://github.com/felipecrs/clipboard-sync/commit/c746d0cbc1c9e5c7c574307b2fa96bfeb83cb730