src/main.js
function JSONPRequest(url, timeout = 3000){
    var self = this;
    self.timeout = timeout;
    self.called = false;
    if (window.document) {
        var ts = Date.now();
        self.scriptTag = window.document.createElement('script');
        // url += '&callback=window.__jsonpHandler_' + ts;
        var _url = '';
        if (url && url !== '') {
            _url = queryfy(url, { callback: `window.__jsonpHandler_${ts}` });
        }

        self.scriptTag.src = _url;
        self.scriptTag.type = 'text/javascript';
        self.scriptTag.async = true;

        self.prom = new Promise((resolve, reject) => {
            var functionName = `__jsonpHandler_${ts}`;
            window[functionName] = function(data){
                self.called = true;
                resolve(data);
                self.scriptTag.parentElement.removeChild(self.scriptTag);
                delete window[functionName];
            };
            // reject after a timeout
            setTimeout(() => {
                if (!self.called){
                    reject('Timeout jsonp request ' + ts);
                    self.scriptTag.parentElement.removeChild(self.scriptTag);
                    delete window[functionName];
                }
            }, self.timeout);
        });
        // the append start the call
        window.document.getElementsByTagName('head')[0].appendChild(self.scriptTag);        
    }
}
function JSONPRequest(url, timeout = 3000){
    var self = this;
    self.timeout = timeout;
    self.called = false;
    if (window.document) {
        var ts = Date.now();
        self.scriptTag = window.document.createElement('script');
        // url += '&callback=window.__jsonpHandler_' + ts;
        var _url = '';
        if (url && url !== '') {
            _url = queryfy(url, { callback: `window.__jsonpHandler_${ts}` });
        }

        self.scriptTag.src = _url;
        self.scriptTag.type = 'text/javascript';
        self.scriptTag.async = true;

        self.prom = new Promise((resolve, reject) => {
            var functionName = `__jsonpHandler_${ts}`;
            window[functionName] = function(data){
                self.called = true;
                resolve(data);
                self.scriptTag.parentElement.removeChild(self.scriptTag);
                delete window[functionName];
            };
            // reject after a timeout
            setTimeout(() => {
                if (!self.called){
                    reject('Timeout jsonp request ' + ts);
                    self.scriptTag.parentElement.removeChild(self.scriptTag);
                    delete window[functionName];
                }
            }, self.timeout);
        });
        // the append start the call
        window.document.getElementsByTagName('head')[0].appendChild(self.scriptTag);        
    }
}
function JSONPRequest ( url , timeout = 3000 ) { var self = this ; self . timeout = timeout ; self . called = false ; if ( window . document ) { var ts = Date . now ( ) ; self . scriptTag = window . document . createElement ( 'script' ) ; // url += '&callback=window.__jsonpHandler_' + ts; var _url = '' ; if ( url && url !== '' ) { _url = queryfy ( url , { callback : ` ${ ts } ` } ) ; } self . scriptTag . src = _url ; self . scriptTag . type = 'text/javascript' ; self . scriptTag . async = true ; self . prom = new Promise ( ( resolve , reject ) => { var functionName = ` ${ ts } ` ; window [ functionName ] = function ( data ) { self . called = true ; resolve ( data ) ; self . scriptTag . parentElement . removeChild ( self . scriptTag ) ; delete window [ functionName ] ; } ; // reject after a timeout setTimeout ( ( ) => { if ( ! self . called ) { reject ( 'Timeout jsonp request ' + ts ) ; self . scriptTag . parentElement . removeChild ( self . scriptTag ) ; delete window [ functionName ] ; } } , self . timeout ) ; } ) ; // the append start the call window . document . getElementsByTagName ( 'head' ) [ 0 ] . appendChild ( self . scriptTag ) ; } }
Make a jsonp request, remember only GET
The function create a tag script and append a callback param in querystring.
The promise will be reject after 3s if the url fail to respond

@example
<pre>
request = new JSONPRequest("http://www.someapi.com/asd?somequery=1");
request.then((data) => {});
</pre>
@param {String} url - the url with querystring but without &callback at the end or &function
@param {Number} timeout - ms range for the response
@return {Promise<Object|String>}
Make a jsonp request remember only GET The function create a tag script and append a callback param in querystring . The promise will be reject after 3s if the url fail to respond