Skip to content

Commit af1e447

Browse files
authored
Merge pull request #1307 from lindapaiste/fix/callback-rejection
Bug fix: `callCallback` should reject the returned Promise
2 parents fb1ded3 + 552d16d commit af1e447

File tree

1 file changed

+26
-5
lines changed

1 file changed

+26
-5
lines changed

src/utils/callcallback.js

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,38 @@
33
// This software is released under the MIT License.
44
// https://opensource.org/licenses/MIT
55

6+
7+
/**
8+
* Most ml5 methods accept a callback function which will be
9+
* called with the arguments (error, result).
10+
*
11+
* Generic type T describes the type of the result.
12+
* @template T
13+
* @callback ML5Callback<T>
14+
* @param {unknown} error - any error thrown during the execution of the function.
15+
* @param {T} [result] - the expected result, if successful.
16+
* @return {void} - callbacks can have side effects, but should not return a value.
17+
*/
18+
19+
/**
20+
* Generic type T describes the type of the result, ie. the value that the Promise will resolve to.
21+
* @template T
22+
* @param {Promise<T>} promise - the Promise to resolve.
23+
* @param {ML5Callback<T>} [callback] - optional callback function to be called
24+
* with the result or error from the resolved Promise.
25+
* @return {Promise<T>} - returns the underlying Promise, which may be rejected.
26+
*/
627
export default function callCallback(promise, callback) {
7-
if (callback) {
28+
if (!callback) return promise;
29+
return new Promise((resolve, reject) => {
830
promise
931
.then((result) => {
1032
callback(undefined, result);
11-
return result;
33+
resolve(result);
1234
})
1335
.catch((error) => {
1436
callback(error);
15-
return error;
37+
reject(error);
1638
});
17-
}
18-
return promise;
39+
});
1940
}

0 commit comments

Comments
 (0)