1.2.3 • Published 7 years ago

power-fetch v1.2.3

Weekly downloads
23
License
ISC
Repository
github
Last release
7 years ago

Power Fetch for React Native

Power Fetch is built on XMLHttpRequest to allow true cancel/abort of your connection.

1. Create a function to abstract your API call

import fetch, { METHODS, HEADERS, STATUS_CODES } from 'power-fetch'

const getIp = () => {
    return fetch({
        url: 'http://ip.jsontest.com/',
        method: METHODS.GET,
        headers: {
            ...HEADERS.JSON
        },
        numberOfAttempts: 5, // Setting this to 0 will set the fetch to try indefinitely.
        timeBeforeTimeout: 2000,
        timeBetweenAttempts: 5000
    }, callback)
}

2. Create your callback function

Simply resolve or reject to complete your callback. If you reject power-fetch will make another request until your total numberOfAttempts is reached. If you resolve power-fetch will immediately return with the argument passed to the resolve function.

const callback = ({ headers, status, json, resolve, reject }) => {
    switch (status) {
        case STATUS_CODES.OK:
            return resolve(json)
        default:
            return reject('unknown-api-error')
    }
})

3. Use your API call

try {
    const { response, cancel } = getIp()
    const { ip } = await response
    console.log(`My IP is ${ip}`)
} catch (err) {
    console.warn(JSON.stringify(err))
}
1.2.3

7 years ago

1.2.2

7 years ago

1.2.1

7 years ago

1.2.0

7 years ago

1.0.14

7 years ago

1.0.13

7 years ago

1.1.0

7 years ago

1.0.12

7 years ago

1.0.11

7 years ago

1.0.10

7 years ago

1.0.9

7 years ago

1.0.8

7 years ago

1.0.7

7 years ago

1.0.6

7 years ago

1.0.5

7 years ago

1.0.4

7 years ago

1.0.3

7 years ago

1.0.2

7 years ago

1.0.1

7 years ago

1.0.0

7 years ago