Skip to content

Commit 77a5091

Browse files
authored
Make .task methods reflect return values from callbacks (#29)
1 parent 943ade0 commit 77a5091

4 files changed

Lines changed: 18 additions & 14 deletions

File tree

index.d.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ declare namespace tempy {
3939
/**
4040
The temporary path created by the function. Can be asynchronous.
4141
*/
42-
type TaskCallback = (tempPath: string) => Promise<void> | void;
42+
type TaskCallback<ReturnValueType> = (tempPath: string) => Promise<ReturnValueType> | ReturnValueType;
4343
}
4444

4545
declare const tempy: {
@@ -59,7 +59,7 @@ declare const tempy: {
5959
});
6060
```
6161
*/
62-
task: (callback: tempy.TaskCallback, options?: tempy.FileOptions) => Promise<void>;
62+
task: <ReturnValueType>(callback: tempy.TaskCallback<ReturnValueType>, options?: tempy.FileOptions) => Promise<ReturnValueType>;
6363

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

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

138138
/**
139139
Write data to a random temp file.

index.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,9 @@ const writeStream = async (filePath, data) => pipeline(data, fs.createWriteStrea
1818
const createTask = (tempyFunction, {extraArguments = 0} = {}) => async (...arguments_) => {
1919
const [callback, options] = arguments_.slice(extraArguments);
2020
const result = await tempyFunction(...arguments_.slice(0, extraArguments), options);
21-
await callback(result);
21+
const returnValue = await callback(result);
2222
await del(result, {force: true});
23+
return returnValue;
2324
};
2425

2526
module.exports.file = options => {

readme.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ Get a temporary file path you can write to.
3737

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

40-
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.
40+
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.
4141

4242
#### callback
4343

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

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

72-
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.
72+
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.
7373

7474
##### callback
7575

@@ -97,7 +97,7 @@ Write data to a random temp file.
9797

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

100-
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.
100+
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.
101101

102102
##### fileContent
103103

test.js

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,11 @@ test('.file()', t => {
3636

3737
test('.file.task()', async t => {
3838
let temporaryFilePath;
39-
await tempy.file.task(async temporaryFile => {
39+
t.is(await tempy.file.task(async temporaryFile => {
4040
await touch(temporaryFile);
4141
temporaryFilePath = temporaryFile;
42-
});
42+
return temporaryFile;
43+
}), temporaryFilePath);
4344
t.false(await pathExists(temporaryFilePath));
4445
});
4546

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

5354
test('.directory.task()', async t => {
5455
let temporaryDirectoryPath;
55-
await tempy.directory.task(async temporaryDirectory => {
56+
t.is(await tempy.directory.task(async temporaryDirectory => {
5657
temporaryDirectoryPath = temporaryDirectory;
57-
});
58+
return temporaryDirectory;
59+
}), temporaryDirectoryPath);
5860
t.false(await pathExists(temporaryDirectoryPath));
5961
});
6062

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

6769
test('.write.task(string)', async t => {
6870
let temporaryFilePath;
69-
await tempy.write.task('', async temporaryFile => {
71+
t.is(await tempy.write.task('', async temporaryFile => {
7072
temporaryFilePath = temporaryFile;
71-
});
73+
return temporaryFile;
74+
}), temporaryFilePath);
7275
t.false(await pathExists(temporaryFilePath));
7376
});
7477

0 commit comments

Comments
 (0)