File tree Expand file tree Collapse file tree 1 file changed +26
-5
lines changed Expand file tree Collapse file tree 1 file changed +26
-5
lines changed Original file line number Diff line number Diff line change 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+ */
627export 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}
You can’t perform that action at this time.
0 commit comments