rethinkdb / rethinkdb-example-nodejs

136 stars 99 forks source link

Unhandled Rejection Error #9

Open codinfox opened 9 years ago

codinfox commented 9 years ago

This issue is about the demo of promise.

It seems that https://github.com/rethinkdb/rethinkdb-example-nodejs/blob/master/todo-angular-express-promise/app.js#L135 and https://github.com/rethinkdb/rethinkdb-example-nodejs/blob/master/todo-angular-express-promise/app.js#L137 can be rejected and since they are not handled by any .error() clause, it is likely to throw an error. I think the right way to do is:

...
        }).finally(function() {
            return r.table('todos').indexCreate('createdAt').run(conn);
        }).finally(function(result) {
            return r.table('todos').indexWait('createdAt').run(conn)
        }).then(function(result) {
...

Or, are these codes written this way on specific purposes?

danielmewes commented 9 years ago

Good catch @codinfox . It doesn't look like it's intentional to me (though I could be wrong).

@deontologician is currently out of office, but he will look into it when he's back.

codinfox commented 9 years ago

Thanks, @danielmewes ! I think what we really care about is just the run result of the last finally (whether indexWait resolved or rejected). So I think the code should be something like:

...
            r.table(dbConn.TABLE_NAME).indexWait(dbConn.INDEX_NAME).run(conn).then(function(){
                conn.close();
                successCallback();
            }).error(function() {
                console.log('Setting up database...');
                // The database/table/index was not available, create them
                r.dbCreate(dbConn.DATABASE_NAME).run(conn).finally(function() {
                    return r.tableCreate(dbConn.TABLE_NAME).run(conn);
                }).finally(function() {
                    return r.table(dbConn.TABLE_NAME).indexCreate(dbConn.INDEX_NAME).run(conn);
                }).catch(function(){})/*We swallow the results of the above promises*/.finally(function() {
                    return r.table(dbConn.TABLE_NAME).indexWait(dbConn.INDEX_NAME).run(conn);
                }).then(successCallback).error(function(err) {
                    console.log('Could not wait for the completion of indexing.')
                    console.log(err.message);
                    process.exit(1);
                }).finally(conn.close);
            });
...