Skip to content

Commit

Permalink
Revert "Improve ora.promise() (sindresorhus#181)"
Browse files Browse the repository at this point in the history
This reverts commit 9c01990.
  • Loading branch information
x71c9 committed Mar 15, 2022
1 parent 5c94f82 commit 27d855d
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 92 deletions.
28 changes: 6 additions & 22 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,22 +133,6 @@ declare namespace ora {
readonly prefixText?: string | PrefixTextGenerator;
}

interface PromiseOptions<T> extends Options {
/**
The new text of the spinner when the promise is resolved.
If undefined, will keep the initial text.
*/
successText?: string | ((result: T) => string);

/**
The new text of the spinner when the promise is rejected.
If undefined, will keep the initial text.
*/
failText?: string | ((error: Error) => string);
}

interface Ora {
/**
A boolean of whether the instance is currently spinning.
Expand Down Expand Up @@ -275,19 +259,19 @@ declare const ora: {
}, 1000);
```
*/
(options?: string | ora.Options): ora.Ora;
(options?: ora.Options | string): ora.Ora;

/**
Starts a spinner for a function or a promise. The spinner is stopped with `.succeed()` if the promise fulfills or with `.fail()` if it rejects. Returns the Promise.
Starts a spinner for a promise. The spinner is stopped with `.succeed()` if the promise fulfills or with `.fail()` if it rejects.
@param action - The promise to start the spinner for.
@param options - If a string is provided, it is treated as a shortcut for `options.text`.
@returns The spinner instance.
*/
promise<T>(
action: PromiseLike<T> | ((spinner: ora.Ora) => PromiseLike<T>),
options?: string | ora.PromiseOptions<T>
): Promise<T>;
promise(
action: PromiseLike<unknown>,
options?: ora.Options | string
): ora.Ora;
};

export = ora;
42 changes: 15 additions & 27 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -393,35 +393,23 @@ const oraFactory = function (options) {

module.exports = oraFactory;

// https://github.com/sindresorhus/ora/issues/169#issuecomment-873269524
module.exports.promise = async (action, options) => {
const actionIsFunction = typeof action === 'function';
module.exports.promise = (action, options) => {
// eslint-disable-next-line promise/prefer-await-to-then
const actionIsPromise = typeof action.then === 'function';

if (!actionIsFunction && !actionIsPromise) {
throw new TypeError('Parameter `action` must be a Function or a Promise');
if (typeof action.then !== 'function') {
throw new TypeError('Parameter `action` must be a Promise');
}

const {successText, failText} = typeof options === 'object' ?
options :
{successText: undefined, failText: undefined};

const spinner = new Ora(options); // Set the initial string or ora options.
const spinner = new Ora(options);
spinner.start();
try {
const promise = actionIsFunction ? action(spinner) : action;
const result = await promise;
spinner.succeed(successText === undefined ?
undefined :
(typeof successText === 'string' ? successText : successText(result))
);
return result;
} catch (error) {
spinner.fail(failText === undefined ?
undefined :
(typeof failText === 'string' ? failText : failText(error))
);
throw error;
}

(async () => {
try {
await action;
spinner.succeed();
} catch {
spinner.fail();
}
})();

return spinner;
};
20 changes: 2 additions & 18 deletions index.test-d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,27 +45,11 @@ spinner.render();
spinner.frame();

const resolves = Promise.resolve(1);
void promise(resolves, 'foo');
void promise(resolves, {
promise(resolves, 'foo');
promise(resolves, {
stream: new PassThroughStream(),
text: 'foo',
color: 'blue',
isEnabled: true,
isSilent: false
});
void promise(async () => {
await resolves;
}, 'foo');
void promise(async spinner => {
spinner.prefixText = 'foo';
await resolves;
return 7;
}, {
stream: new PassThroughStream(),
text: 'foo',
color: 'blue',
isEnabled: true,
isSilent: false,
successText: result => `Resolved with number ${result}`,
failText: 'bar'
});
21 changes: 2 additions & 19 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -226,28 +226,11 @@ Change the spinner indent.
### ora.promise(action, text)
### ora.promise(action, options)

Starts a spinner for a function or a promise. The spinner is stopped with `.succeed()` if the promise fulfills or with `.fail()` if it rejects. Returns the promise.
Starts a spinner for a promise. The spinner is stopped with `.succeed()` if the promise fulfills or with `.fail()` if it rejects. Returns the spinner instance.

#### action

Type: `Promise | ((spinner: ora.Ora) => Promise)`

#### options

Type: `object`

All of the [options](#options) plus the following:

##### successText
Type: `string | ((result: T) => string)`

The new text of the spinner when the promise is resolved. If undefined, will keep the initial text.

##### failText
Type: `string | ((error: Error) => string)`

The new text of the spinner when the promise is rejected. If undefined, will keep the initial text.

Type: `Promise`

## FAQ

Expand Down
14 changes: 8 additions & 6 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -169,13 +169,15 @@ test('.promise() - rejects', async t => {
const output = getStream(stream);
const rejects = Promise.reject(new Error()); // eslint-disable-line unicorn/error-message

Ora.promise(rejects, {
stream,
text: 'foo',
color: false,
isEnabled: true
});

try {
await Ora.promise(rejects, {
stream,
text: 'foo',
color: false,
isEnabled: true
})
await rejects;
} catch {}

stream.end();
Expand Down

0 comments on commit 27d855d

Please sign in to comment.