Alasql: Promise not working in browser

Created on 24 Sep 2015  ยท  18Comments  ยท  Source: agershun/alasql

The method .promise on the alasql object is not present when running in a browser.

Recreate: make a .html document with the following content:

<script src="http://cdn.jsdelivr.net/alasql/0.2/alasql.min.js"></script> 
<script>
    alasql.promise('SELECT 123 as abc ')
            .then(function(res){
                console.log('all OK');
            }).catch(function(err){
                console.log('not good');
            });
</script>

this gives an Uncaught TypeError: undefined is not a function on the line with alasql.promise('SELECT 123 as abc '). I have tried to include the promise polyfill from https://github.com/taylorhakes/promise-polyfill/blob/master/Promise.min.js but it did not solve the issue.


The same code gives an all OK in node:

alasql = require('alasql');
alasql.promise('SELECT 123 as abc ')
  .then(function(res){
  console.log('all OK');
}).catch(function(err){
  console.log('not good');
});
// all OK
! Bug

All 18 comments

Just started playing with alasql - looks great!

I'm running into this issue as well. Any timeline for when this might be resolved? Any workarounds?

You can use the

 alasql('select abc from test', [],  function(data,err) {
                    if(err) {
                     console.log(err);
                 } 
                    // all good
})

But its still an issue...

Looking into the source I see

if(typeof exports === 'object') {
    var Promise = require('es6-promise').Promise;
} 

//
// Only for browsers with Promise support
//

if(typeof Promise === 'function') {
    alasql.promise = function(sql, params) {
        return new Promise(function(resolve, reject){
            alasql(sql, params, function(data,err) {
                 if(err) {
                     reject(err);
                 } else {
                     resolve(data);
                 }
            });
        });
    };  
}

so it should work - but but but - looks like even including bluebird or es6-promise before including alasql then the Promise is not defined yet.

A poopy workaround would be the following:

<script src="https://cdn.rawgit.com/jakearchibald/es6-promise/master/dist/es6-promise.min.js"></script> 

<script src="https://cdn.rawgit.com/agershun/alasql/f4a38bf4f1e16969030612f3bc13a2a5c40789af/dist/alasql.js"></script> 


<script>
// This is part of the alasql code - but its ran before Promise is defined - so we run it again (crazy world...)
if(typeof Promise === 'function') {
    alasql.promise = function(sql, params) {
        return new Promise(function(resolve, reject){
            alasql(sql, params, function(data,err) {
                 if(err) {
                     reject(err);
                 } else {
                     resolve(data);
                 }
            });
        });
    };  
}

 alasql.promise('SELECT 123 as abc ')
    .then(function(res){
        console.log('all OK');
    }).catch(function(err){
        console.log('not good');
    });

</script>

Doesn't work with polyfil from promise wiki page. (Safari, Chrome) =(

What version of safari or chrome are you using?

Safari 9.0.3 on El Capitan
Chrome 43 for Mac

Hmmm. Interesting.

Could I ask you to try with this version included?

<script src="https://rawgit.com/agershun/alasql/develop/dist/alasql.js"></script>

Didn't work on FF 45.0.1 on Windows 10
Works with developer version you linked to

@seb-ster Just to be sure I understand: with the last version its now working with FF 45 - right?

Yes.
[edit]
Just downloaded AlaSQL v0.2.4-develop-1241 just to make sure i'm using the latest version.
And it works.

I'm using Yeoman for scaffolding an AngularJS app. Project dependencies are managed with bower. Here you can see the versions of alasql and es6-promise:

bower check-new     Checking for new versions of the project dependencies...
dis1#0.0.0 /Users/boutinov/Sites/dis1
โ”œโ”€โ”ฌ alasql#0.2.4
โ”‚ โ”œโ”€โ”€ es6-promise#3.0.2 (latest is 3.2.1)
โ”‚ โ”œโ”€โ”€ js-xls#0.7.5
โ”‚ โ””โ”€โ”€ js-xlsx#0.8.0
...

In index.html both alasql and es6-promise were included all this time:

    <script src="bower_components/es6-promise/promise.js"></script>
    <script src="bower_components/alasql/dist/alasql.min.js"></script>

Mathias, correct me if I'm wrong, I should switch to alasql 0.2.4-DEVELOP to have promise notation work?

Here's a sample:

<script src="bower_components/es6-promise/promise.js"></script>
<script src="bower_components/alasql/dist/alasql.js"></script>
<script>
    alasql.promise('SELECT 123 as abc ')
            .then(function(res){
                console.log('all OK');
            }).catch(function(err){
                console.log('not good');
            });
</script>

Versions:

alasql#0.2.4 bower_components/alasql
โ”œโ”€โ”€ es6-promise#3.0.2
โ”œโ”€โ”€ js-xls#0.7.5
โ””โ”€โ”€ js-xlsx#0.8.0

This gives an error in Safari 9 Mac, Chrome 43 Mac, Firefox 38 Mac, Opera 35 Mac:

TypeError: undefined is not a constructor (evaluating 'new Promise')
promisealasql.js:4422

But, as @seb-ster already said, there is no error when you change alasql's source to
<script src="https://rawgit.com/agershun/alasql/develop/dist/alasql.js"></script>.
And so I answered my question from my previous comment myself)

Thank you for awesome description.

I have pushed the latest code as a new release so everyone can benefit from 0.2.5 now.

I have the following versions of:
"bower_components/es6-promise/promise.min.js" (0.2.7)
"bower_components/alasql/console/alasql.min.js" (3.0.2)
"bower_components/alasql/console/xlsx.core.min.js" (0.8.0)

When working without promise it results OK. But with promise I get this error:

angular.js:12798 TypeError: Q is not a constructor
at Function.T.promise (alasql.min.js:9)
.....

My controller has this:

//xlsdata is well defined (works ok without promise)....
alasql.promise("SELECT * INTO XLSX('myfile.xlsx',{headers:true}) FROM ? ",[xlsdata])
.then(function(data){
console.log('Data saved');
}).catch(function(err){
console.log('Failed');
}); // <--doesn't work

//alasql("SELECT * INTO XLSX('myfile.xlsx',{headers:true}) FROM ? ",[xlsdata]); // <-- works ok

PLEASE ADVISE!

Questions:

  • Why is the version number for alasql 3.0.2?
  • What value do you get from alasql.version?
  • Have you tried without including the promise.min.js part?
  • Can you create a minimum version of the code replicating the problem that you can share?

I included the following fragment before the promise call and it worked. Thanks:

if(typeof Promise === 'function') { alasql.promise = function(sql, params) { return new Promise(function(resolve, reject){ alasql(sql, params, function(data,err) { if(err) { reject(err); } else { resolve(data); } }); }); }; }

Alonso M.

On Mon, Jul 25, 2016 at 4:21 PM -0500, "Mathias Rangel Wulff" <[email protected]notifications@github.com> wrote:

Questions:

  • Why is the version number for alasql 3.0.2?
  • What value do you get from alasql.version?
  • Have you tried without including the promise.min.js part?
  • Can you create a minimum version of the code replicating the problem that you can share?

You are receiving this because you commented.
Reply to this email directly, view it on GitHubhttps://github.com/agershun/alasql/issues/426#issuecomment-235089282, or mute the threadhttps://github.com/notifications/unsubscribe-auth/ATrcmGvzQGByhCUh3EYxAF1zoMMGHwe-ks5qZSjbgaJpZM4GC9Bk.

Alonso M.

De: Mathias Rangel Wulff
Enviado: lunes, 25 de julio 4:21 PM
Asunto: Re: [agershun/alasql] Promise not working in browser (#426)
Para: agershun/alasql
Cc: amontezumag, Comment

Questions:

Why is the version number for alasql 3.0.2?

What value do you get from alasql.version?

Have you tried without including the promise.min.js part?

I ACTUALLY HAD TO INCLUDE THIS PART TO GET THING WORKINGS. Thanks!

Can you create a minimum version of the code replicating the problem that you can share?

You are receiving this because you commented.
Reply to this email directly, view it on https://github.com/agershun/alasql/issues/426#issuecomment-235089282 GitHubhttps://github.com/agershun/alasql/issues/426#issuecomment-235089282, or mute the threadhttps://github.com/notifications/unsubscribe-auth/ATrcmGvzQGByhCUh3EYxAF1zoMMGHwe-ks5qZSjbgaJpZM4GC9Bk.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

cscan picture cscan  ยท  4Comments

umasudhan picture umasudhan  ยท  4Comments

mathiasrw picture mathiasrw  ยท  5Comments

mwhebert picture mwhebert  ยท  3Comments

Serge-SDL picture Serge-SDL  ยท  4Comments