I am working on a custom button control that has a click handler which can either return a promise or void. Here is an example of the button options interface and the click handler:
// --- Options for button control
export interface buttonOptions {
aProperty: string
anotherProperty: number
onClick: Promise<IButtonClickResult> | void
}
// --- Click handler added for click event inside control class
protected clickHandler(): Promise<IButtonClickResult> | void {
if(returnsPromise) displaySpinner()
various operations
this.options.onClick(this)
}
To create the control using the builder pattern, you would do the following:
buttonClass.build()
.options({
aProperty: 'Hope this works',
anotherProperty: 1,
onClick: () => {
return new Promise<IButtonClickResult>((resolve, reject) => {
// --- I suppose I could pass reference to resolve or reject to async function and allow that function to resolve or reject the promise
asyncOperation(resolve, reject)
}).then((result) => button.onClickSuccess(result)).catch((error) => button.onclickError(error))
You can also assign a function to onClick that returns void:
console.log('I was clicked')
If the click handler returns a promise, I want to show a spinner on the button until the promise is resolved or rejected. My question is how can I determine if a promise will be returned so I know when to display the spinner. Any guidance on this issue would be greatly appreciated. I have tried using typeof and instanceof, but I have not been successful so far. I might not be using them correctly as I am still new to TypeScript and NodeJS.