[refactor] bump version
parent
cc175aca05
commit
10ae6427ec
26
index.js
26
index.js
|
@ -9,25 +9,25 @@ let count = 0;
|
||||||
*
|
*
|
||||||
* Options:
|
* Options:
|
||||||
* - prefix {String} callback prefix (defaults to `__jp`)
|
* - prefix {String} callback prefix (defaults to `__jp`)
|
||||||
* - jsonp {String} qs jsonpeter (defaults to `callback`)
|
* - callback {String} (defaults to `callback`)
|
||||||
* - timeout {Number} how long after the request until a timeout error
|
* - timeout {Number} how long after the request until a timeout error
|
||||||
* is emitted (defaults to `15000`)
|
* is emitted (defaults to `15000`)
|
||||||
*/
|
*/
|
||||||
let jsonp = function( url, options ) {
|
let jsonp = function( url, options ) {
|
||||||
options = options || {};
|
options = options || {};
|
||||||
let prefix = options.prefix || '__jp';
|
let prefix = options.prefix || '__jp';
|
||||||
let jsonp = options.jsonp || 'callback';
|
let callback = options.callback || 'callback';
|
||||||
let params = options.params || {};
|
let params = options.data || {};
|
||||||
let timeout = options.timeout ? options.timeout : 15000;
|
let timeout = options.timeout ? options.timeout : 15000;
|
||||||
let target = document.getElementsByTagName( 'script' )[ 0 ] || document.head;
|
let target = document.getElementsByTagName( 'script' )[ 0 ] || document.head;
|
||||||
let script;
|
let script;
|
||||||
let timer;
|
let timer;
|
||||||
let cleanup;
|
let cleanup;
|
||||||
let promise;
|
let promise;
|
||||||
let noop = function() {};
|
let noop = function() {};
|
||||||
// Generate a unique id for the request.
|
// Generate a unique id for the request.
|
||||||
let id = prefix + (count++);
|
let id = prefix + (count++);
|
||||||
cleanup = function() {
|
cleanup = function() {
|
||||||
// Remove the script tag.
|
// Remove the script tag.
|
||||||
if ( script && script.parentNode ) {
|
if ( script && script.parentNode ) {
|
||||||
script.parentNode.removeChild( script );
|
script.parentNode.removeChild( script );
|
||||||
|
@ -35,18 +35,18 @@ let jsonp = function( url, options ) {
|
||||||
window[ id ] = noop;
|
window[ id ] = noop;
|
||||||
if ( timer ) { clearTimeout( timer ); }
|
if ( timer ) { clearTimeout( timer ); }
|
||||||
};
|
};
|
||||||
promise = new Promise( ( resolve, reject ) => {
|
promise = new Promise( ( resolve, reject ) => {
|
||||||
if ( timeout ) {
|
if ( timeout ) {
|
||||||
timer = setTimeout( function() {
|
timer = setTimeout( function() {
|
||||||
cleanup();
|
cleanup();
|
||||||
reject( new Error( 'Timeout' ) );
|
reject( new Error( 'Timeout' ) );
|
||||||
}, timeout );
|
}, timeout );
|
||||||
}
|
}
|
||||||
window[ id ] = function( data ) {
|
window[ id ] = function( data ) {
|
||||||
cleanup();
|
cleanup();
|
||||||
resolve( data );
|
resolve( data );
|
||||||
};
|
};
|
||||||
params[ jsonp ] = id;
|
params[ callback ] = id;
|
||||||
for ( let key in params ) {
|
for ( let key in params ) {
|
||||||
url += (~url.indexOf( '?' ) ? '&' : '?') + key + '=' + encodeURIComponent( params[ key ] );
|
url += (~url.indexOf( '?' ) ? '&' : '?') + key + '=' + encodeURIComponent( params[ key ] );
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
{
|
{
|
||||||
"name": "simple-jsonp-promise",
|
"name": "simple-jsonp-promise",
|
||||||
"version": "1.0.0",
|
"version": "1.0.1",
|
||||||
"description": "",
|
"description": "",
|
||||||
"main": "index.js",
|
"main": "index.js",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
|
|
Loading…
Reference in New Issue