Skip to Content
Menu

fetch()

A wrapper for the JavaScript Fetch API that simplifies the pattern for basic calls.

JavaScript May 29, 2022

Usage

JavaScript
nebula.fetch(url, headers, type).then(function(data){...})

Parameters

url
(Required) (String) The endpoint URL for the data to be fetched
Default: None

headers
(Optional) (Object) Optional fetch headers
Default: None

type
(Optional) (String) The expected data type for processing (json or text)
Default: json

data
(Required) (Mixed) The fetched data response
Default: None

Parameter Notes

If no headers are needed, you can pass the type as the second parameter.

Request or provide clarification »

Examples

Simple JSON fetch

JavaScript
nebula.fetch('https://example.com/data.json').then(function(json){
    console.log('here is the fetched json:', json);
});

A text fetch with no-cache headers

JavaScript
nebula.fetch('https://example.com/data.json', {
    cache: 'no-cache'
}, 'text').then(function(data){
    console.log('here is the fetched text data:', data);
});

Fetching text data without passing a headers object

JavaScript
nebula.fetch('https://example.com/data.json', 'text').then(function(data){
    console.log('here is the fetched text data:', data);
});

Additional Notes

For more complicated patterns, you may want to use the Fetch API directly, but this Nebula function simplifies the traditional fetch() pattern significantly for simple tasks.

Was this page helpful? Yes No


    A feedback message is required to submit this form.


    Please check that you have entered a valid email address.

    Enter your email address if you would like a response.

    Thank you for your feedback!

    Source File

    Located in /assets/js/modules/utilities.js on line 256.

    No Hooks

    This function does not have any filters or actions available. Request one?
    JavaScript
    nebula.fetch = async function(url=false, headers={}, type='json'){
        if ( !url ){
            nebula.help('nebula.fetch() requires a URL to retreive.', '/functions/fetch/');
            return false;
        }
    
        if ( typeof headers !== 'object' ){ //If the type is passed as the second parameter
            type = headers;
            headers = {};
        }
    
        let fetchedData = await fetch(url, headers).then(function(response){
            if ( response.ok ){
                if ( type === 'json' ){
                    return response.json();
                }
    
                return response.text();
            }
        }).then(function(json){
            return json;
        });
    
        return fetchedData;
    };
    

    Override

    To override or disable this JavaScript function, simply redeclare it with the exact same function name. Remember: Some functionality is conditionally loaded via dynamic imports, so if your function is not overriding properly, try listening for a DOM event (described below).

    JavaScript

    For non-module import functions:

    nebula.fetch = function(url, headers, type, data){
        //Write your own code here, leave it blank, or return false.
    }


    For dynamically imported module function overrides:

    jQuery(window).on('load', function(){
        nebula.fetch = function(url, headers, type, data){
            //Write your own code here, leave it blank, or return false.
        }
    });


    Custom Nebula DOM events do also exist, so you could also try the following if the Window Load listener does not work:

    jQuery(document).on('nebula_module_loaded', function(module){
        //Note that the module variable is also available to know which module specifically was imported
        if ( typeof nebula.fetch === 'function' ){
            nebula.fetch = function(url, headers, type, data){
                //Write your own code here, leave it blank, or return false.
            }
    	}
    });