• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

ckeditor / ckeditor5-react / ca6e1779-8fe0-4d9b-af01-91100ac4e4bc

24 Sep 2024 01:45PM UTC coverage: 100.0%. Remained the same
ca6e1779-8fe0-4d9b-af01-91100ac4e4bc

Pull #536

circleci

martnpaneq
Updated dependency versions to remove warnings.
Pull Request #536: Updated dependency versions to remove warnings.

281 of 281 branches covered (100.0%)

Branch coverage included in aggregate %.

582 of 582 relevant lines covered (100.0%)

70.24 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

100.0
/src/hooks/useAsyncValue.ts
1
/**
2
 * @license Copyright (c) 2003-2024, CKSource Holding sp. z o.o. All rights reserved.
3
 * For licensing, see LICENSE.md.
4
 */
5

6
import type { DependencyList } from 'react';
7

8
import { useInstantEffect } from './useInstantEffect.js';
9
import { useAsyncCallback, type AsyncCallbackState } from './useAsyncCallback.js';
10

11
/**
12
 * A hook that allows to execute an asynchronous function and provides the state of the execution.
13
 * The asynchronous function is executed immediately after the component is mounted.
14
 *
15
 * @param callback The asynchronous function to be executed.
16
 * @param deps The dependency list.
17
 * @returns The state of the execution.
18
 *
19
 * @example
20
 * ```tsx
21
 * const asyncFetchState = useAsyncValue( async () => {
22
 *         const response = await fetch( 'https://api.example.com/data' );
23
 *         const data = await response.json();
24
 *         return data;
25
 * }, [] );
26
 *
27
 * if ( asyncFetchState.status === 'loading' ) {
28
 *         return <p>Loading...</p>;
29
 * }
30
 *
31
 * if ( asyncFetchState.status === 'success' ) {
32
 *         return <pre>{ JSON.stringify( asyncFetchState.data, null, 2 ) }</pre>;
33
 * }
34
 *
35
 * if ( asyncFetchState.status === 'error' ) {
36
 *         return <p>Error: { asyncFetchState.error.message }</p>;
37
 * }
38
 * ```
39
 */
40
export const useAsyncValue = <A extends Array<unknown>, R>(
4✔
41
        callback: ( ...args: Array<A> ) => Promise<R>,
42
        deps: DependencyList
43
): AsyncValueHookResult<R> => {
44
        const [ asyncCallback, asyncState ] = useAsyncCallback( callback );
45✔
45

46
        useInstantEffect( asyncCallback, deps );
45✔
47

48
        // There might be short delay between the effect and the state update.
49
        // So it is possible that the status is still 'idle' after the effect.
50
        // In such case, we should return 'loading' status because the effect is already queued to be executed.
51
        if ( asyncState.status === 'idle' ) {
45✔
52
                return {
13✔
53
                        status: 'loading'
54
                };
55
        }
56

57
        return asyncState;
32✔
58
};
59

60
/**
61
 * The result of the `useAsyncValue` hook.
62
 */
63
export type AsyncValueHookResult<R> = Exclude<AsyncCallbackState<R>, { status: 'idle' }>;
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc