GithubHelp home page GithubHelp logo

isabella232 / fetch-script Goto Github PK

View Code? Open in Web Editor NEW

This project forked from adobe-marketing-cloud/fetch-script

0.0 0.0 0.0 26 KB

Load JavaScript files like HTML 5 Fetch API

License: Apache License 2.0

JavaScript 100.00%

fetch-script's Introduction

Script loading is NOT supported in standard Fetch API, https://fetch.spec.whatwg.org. fetch-script provides you the API to fetch script files that is similar to native Fetch API. This library has been inspired by fetch-jsonp

Installation

You can install with npm.

npm install fetch-script

Usage

The fetchScript function supports ONLY GET HTTP method.

Fetch script in simple way

import fetchScript from 'fetch-script';

const fetch = fetchScript();

fetch('/some-javascript-file.js')
.then(function(response) {
  console.log('script loaded successfully');
})
.catch(function(ex) {
  console.log('failed', ex);
});

Set script request timeout, default is 5000ms.

import fetchScript from 'fetch-script';

const fetch = fetchScript();

fetch('/some-javascript-file.js', {timeout: 3000})
.then(function(response) {
  console.log('script loaded successfully');
})
.catch(function(ex) {
  console.log('failed', ex);
});

NOTE: timeout option is not supported in the native Fetch API.

Passing Promise implementation, default is window.Promise, make sure you pass custom implementation if the browser doesn't support ES6 Promise.

import fetchScript from 'fetch-script';

const fetch = fetchScript({Promise: CustomPromise});

fetch('/some-javascript-file.js', {timeout: 3000})
.then(function(response) {
  console.log('script loaded successfully');
})
.catch(function(ex) {
  console.log('failed', ex);
});

Caveats

Since we can't retrieve loaded script content, the response will contain just the ok property set to true. This is to be consistent with Fetch API. You can safely ignore response parameter.

Browser Support

Chrome Firefox IE Opera Safari
Latest ✔ Latest ✔ 9+ ✔ Latest ✔ 6.1+ ✔

fetch-script's People

Contributors

artur-ciocanu avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.