remote data
1.0.0
원격 소스(HTTP 포함)에서 데이터를 가져오기 위한 작은(450바이트 미만) 도구입니다. 원격 데이터가 왜 도움이 되는지 자세히 알아보려면 이 게시물을 읽어보세요.
$ npm install @roebuk/remote-data
< script type =" module " >
import RemoteData from 'https://cdn.skypack.dev/@roebuk/remote-data' ;
</ script >
< script src =" https://unpkg.com/@roebuk/remote-data " > </ script >
API 문서
import * as RemoteData from '@roebuk/remote-data' ;
// Set the initial state
var remoteUsers = RemoteData . NotAsked ( ) ;
// An interaction starts off the request
remoteUsers = RemoteData . Loading ( ) ;
// Once the request is complete,
// it will either be in a `Success` or `Failure` state.
remoteUsers = await fetch ( '/api/users' )
. then ( res => res . json ( ) )
. then ( users => RemoteData . Success ( users ) )
. catch ( err => RemoteData . Failure ( err ) ) ;
// "Pattern match" on the RemoteData type and extract the current state.
// The return value of the functions should all be of the same type.
RemoteData . match ( {
notAsked : ( ) => 'Not Requested the data'
loading : ( ) = > 'Loading...'
success : users => `Loaded ${ users . length } users` ,
failed : err => `Something when wrong. Details: ${ err . message } `
} , remoteUsers ) ;
StackBlitz의 반응 및 원격 데이터
npm ci
npm run build
npm t