-
-
Notifications
You must be signed in to change notification settings - Fork 270
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Suggestion: promise enhancements #169
Comments
Sounds like a good idea in general. I agree it would be better if it returned the promise instead of the instance. I'm not sure why it's beneficial for it to accept a promise-returning function over just a promise though. |
I agree with the suggestion to enhance the Follow the code: Line 394 in 498c40a
It can work with a single action only and it is not different between using the
It is a great and different way to use the async Emmm... the example as above is a simple flow to use |
Couldn't Edit: Also, I agree. It should return a Promise so I can await it. Edit2: Here is my implementation of oraPromise, based on the @sarink code: type OraActionType<T> = Promise<T> | ((spinner: ora.Ora) => Promise<T>);
type OraPromiseOptions<T> = {
/** The new text of the spinner when the promise is resolved.
*
* * If undefined, will keep the initial text. */
successText?: ((resp: T) => string) | string;
/** The new text of the spinner when the promise is rejected.
*
* If undefined, will keep the initial text. */
failText?: ((err: Error) => string) | string;
} & ora.Options
export async function oraPromise<T>(action: OraActionType<T>, text: string): Promise<T>;
export async function oraPromise<T>(action: OraActionType<T>, options: OraPromiseOptions<T>): Promise<T>;
export async function oraPromise<T>(
action: OraActionType<T>, complement: string | OraPromiseOptions<T>,
): Promise<T> {
const { successText, failText } = typeof complement === 'string'
? { successText: undefined, failText: undefined }
: complement;
const spinner = ora(complement); // Set the initial string or ora options.
spinner.start();
try {
const promise = typeof action === 'function' ? action(spinner) : action;
const result = await promise;
spinner.succeed(
successText === undefined
? undefined
: (typeof successText === 'string' ? successText : successText(result)),
);
return result;
} catch (err) {
spinner.fail(
failText === undefined
? undefined
: (typeof failText === 'string' ? failText : failText(err)),
);
throw err;
}
} Besides the initial OP functionalities (a little improved), this will call the promise function if one is passed, allows further customizations by using Edit3: The OP Edit4: Added the I will later make a PR for it as I think it's very good and useful. |
Created PR @ #181 |
I often want to display different text based on the result of the
promise
(whether success or failure). Sometimes myaction
is multiple promises, so it would be convenient if the signature for that was() => Promise
(so that I don't need to do create a new named variable for my promise on the line before it, and then pass it to ora). Lastly, it seems a lot more convenient to return the promise, rather than the spinner instance.I've been using this little wrapper
I think it enhances the usability of
promise
for some common cases. If you think it's worthwhile, I may be able to find time to submit a PR.example:
The text was updated successfully, but these errors were encountered: