Jsonp es6 implementation that returns a promise.
npm install jsonp-promisehttps://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.
npm:``bash`
$ npm install jsonp-promise`
orbash`
$ jspm install npm:jsonp-promise
###jsonp(url, options)
- url (String) url to fetchoptions
- (Object), optionalparam
- (String) name of the query string parameter to specifycallback
the callback (defaults to )timeout
- (Number) how long after the request until a timeout error0
is emitted. to disable (defaults to 15000)prefix
- (String) prefix for the global callback functions that__jp
handle jsonp responses (defaults to )
Returns an object containing two properties:
- promise a promise which will be resolved if the jsonp request succeeds,cancel
otherwise will be rejected
- 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.
`javascript``
// 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);
}
MIT