Skip to content
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

Make .task methods reflect return values from callbacks #29

Merged
merged 1 commit into from
Oct 12, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ declare namespace tempy {
/**
The temporary path created by the function. Can be asynchronous.
*/
type TaskCallback = (tempPath: string) => Promise<void> | void;
type TaskCallback<ReturnValueType> = (tempPath: string) => Promise<ReturnValueType> | ReturnValueType;
}

declare const tempy: {
Expand All @@ -59,7 +59,7 @@ declare const tempy: {
});
```
*/
task: (callback: tempy.TaskCallback, options?: tempy.FileOptions) => Promise<void>;
task: <ReturnValueType>(callback: tempy.TaskCallback<ReturnValueType>, options?: tempy.FileOptions) => Promise<ReturnValueType>;

/**
Get a temporary file path you can write to.
Expand Down Expand Up @@ -99,7 +99,7 @@ declare const tempy: {
})
```
*/
task: (callback: tempy.TaskCallback, options?: tempy.DirectoryOptions) => Promise<void>;
task: <ReturnValueType>(callback: tempy.TaskCallback<ReturnValueType>, options?: tempy.DirectoryOptions) => Promise<ReturnValueType>;

/**
Get a temporary directory path. The directory is created for you.
Expand Down Expand Up @@ -133,7 +133,7 @@ declare const tempy: {
});
```
*/
task: (fileContent: string | Buffer | TypedArray | DataView | NodeJS.ReadableStream, callback: tempy.TaskCallback, options?: tempy.FileOptions) => Promise<void>;
task: <ReturnValueType>(fileContent: string | Buffer | TypedArray | DataView | NodeJS.ReadableStream, callback: tempy.TaskCallback<ReturnValueType>, options?: tempy.FileOptions) => Promise<ReturnValueType>;

/**
Write data to a random temp file.
Expand Down
3 changes: 2 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@ const writeStream = async (filePath, data) => pipeline(data, fs.createWriteStrea
const createTask = (tempyFunction, {extraArguments = 0} = {}) => async (...arguments_) => {
const [callback, options] = arguments_.slice(extraArguments);
const result = await tempyFunction(...arguments_.slice(0, extraArguments), options);
await callback(result);
const returnValue = await callback(result);
await del(result, {force: true});
return returnValue;
};

module.exports.file = options => {
Expand Down
6 changes: 3 additions & 3 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ Get a temporary file path you can write to.

### tempy.file.task(callback, options?)

The `callback` resolves with a temporary file path you can write to. The file is automatically cleaned up after the callback is executed. Returns a promise that resolves after the callback is executed and the file is cleaned up.
The `callback` resolves with a temporary file path you can write to. The file is automatically cleaned up after the callback is executed. Returns a promise that resolves with the return value of the callback after it is executed and the file is cleaned up.

#### callback

Expand Down Expand Up @@ -69,7 +69,7 @@ Get a temporary directory path. The directory is created for you.

### tempy.directory.task(callback, options?)

The `callback` resolves with a temporary directory path you can write to. The directory is automatically cleaned up after the callback is executed. Returns a promise that resolves after the callback is executed and the directory is cleaned up.
The `callback` resolves with a temporary directory path you can write to. The directory is automatically cleaned up after the callback is executed. Returns a promise that resolves with the return value of the callback after it is executed and the directory is cleaned up.

##### callback

Expand Down Expand Up @@ -97,7 +97,7 @@ Write data to a random temp file.

### tempy.write.task(fileContent, callback, options?)

Write data to a random temp file. The file is automatically cleaned up after the callback is executed. Returns a promise that resolves after the callback is executed and the file is cleaned up.
Write data to a random temp file. The file is automatically cleaned up after the callback is executed. Returns a promise that resolves with the return value of the callback after it is executed and the file is cleaned up.

##### fileContent

Expand Down
15 changes: 9 additions & 6 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,11 @@ test('.file()', t => {

test('.file.task()', async t => {
let temporaryFilePath;
await tempy.file.task(async temporaryFile => {
t.is(await tempy.file.task(async temporaryFile => {
await touch(temporaryFile);
temporaryFilePath = temporaryFile;
});
return temporaryFile;
}), temporaryFilePath);
t.false(await pathExists(temporaryFilePath));
});

Expand All @@ -52,9 +53,10 @@ test('.directory()', t => {

test('.directory.task()', async t => {
let temporaryDirectoryPath;
await tempy.directory.task(async temporaryDirectory => {
t.is(await tempy.directory.task(async temporaryDirectory => {
temporaryDirectoryPath = temporaryDirectory;
});
return temporaryDirectory;
}), temporaryDirectoryPath);
t.false(await pathExists(temporaryDirectoryPath));
});

Expand All @@ -66,9 +68,10 @@ test('.write(string)', async t => {

test('.write.task(string)', async t => {
let temporaryFilePath;
await tempy.write.task('', async temporaryFile => {
t.is(await tempy.write.task('', async temporaryFile => {
temporaryFilePath = temporaryFile;
});
return temporaryFile;
}), temporaryFilePath);
t.false(await pathExists(temporaryFilePath));
});

Expand Down