Code Monkey home page Code Monkey logo

jsonp-promise's Introduction

jsonp-promise-enhance

FORK

  1. added short hash rather than self incremental callback param

Description

A simple JSONP es6 implementation (inspired from https://github.com/webmodules/jsonp) which returns an object containing a promise, so it can be used by generators and async functions.

It's supposed to be used with an es6 module loader. Works great with jspm/systemjs for cross-browser compatibility.

Instalation

Install for node.js, browserify or jspm using npm:

$ npm install jsonp-promise

or

$ jspm install npm:jsonp-promise

API

###jsonp(url, options)

  • url (String) url to fetch
  • options (Object), optional
    • param (String) name of the query string parameter to specify the callback (defaults to callback)
    • timeout (Number) how long after the request until a timeout error is emitted. 0 to disable (defaults to 15000)
    • prefix (String) prefix for the global callback functions that handle jsonp responses (defaults to __jp)

Returns an object containing two properties:

  • promise a promise which will be resolved if the jsonp request succeeds, otherwise will be rejected
  • cancel a method which will cancel the request and reject the promise

If it times out or gets canceled, the promise will be rejected with an Error object.

// e.g.: get the latest javascript subrredits
async function getJson() {
    let data = await jsonp('http://www.reddit.com/r/javascript/top.json', {param: 'jsonp'}).promise;
    console.log(data.data.children);
}

License

MIT

jsonp-promise's People

Contributors

alexbardas avatar jf3096 avatar

Watchers

James Cloos avatar  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.