Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More promise functions #1896

Merged
merged 3 commits into from Feb 4, 2017
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/dialects/websql/transaction.js
Expand Up @@ -34,7 +34,8 @@ function makeClient(trx, client) {
const promiseInterface = [
'then', 'bind', 'catch', 'finally', 'asCallback',
'spread', 'map', 'reduce', 'tap', 'thenReturn',
'return', 'yield', 'ensure', 'exec', 'reflect'
'return', 'yield', 'ensure', 'exec', 'reflect',
'get', 'mapSeries', 'delay'
]

// Creates a method which "coerces" to a promise, by calling a
Expand Down
10 changes: 5 additions & 5 deletions src/interface.js
@@ -1,6 +1,6 @@

import * as helpers from './helpers';
import { isArray, map, clone, each } from 'lodash'
import { isArray, map, clone, each, identity } from 'lodash'

export default function(Target) {

Expand Down Expand Up @@ -64,11 +64,11 @@ export default function(Target) {
// "then" method on the current `Target`
each(['bind', 'catch', 'finally', 'asCallback',
'spread', 'map', 'reduce', 'tap', 'thenReturn',
'return', 'yield', 'ensure', 'reflect'], function(method) {
'return', 'yield', 'ensure', 'reflect',
'get', 'mapSeries', 'delay'], function(method) {
Target.prototype[method] = function() {
let then = this.then();
then = then[method].apply(then, arguments);
return then;
const promise = this.then(identity);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why identity must be passed here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It doesn't need to be. Before I realize that .then doesn't need a parameter, in order to use bluebird's .get, I've been using .then(_.identity).get(0). I've pushed a commit to remove it.

return promise[method].apply(promise, arguments);
};
});

Expand Down
3 changes: 2 additions & 1 deletion src/transaction.js
Expand Up @@ -243,7 +243,8 @@ function completedError(trx, obj) {
const promiseInterface = [
'then', 'bind', 'catch', 'finally', 'asCallback',
'spread', 'map', 'reduce', 'tap', 'thenReturn',
'return', 'yield', 'ensure', 'exec', 'reflect'
'return', 'yield', 'ensure', 'exec', 'reflect',
'get', 'mapSeries', 'delay'
]

// Creates a method which "coerces" to a promise, by calling a
Expand Down
40 changes: 40 additions & 0 deletions test/integration/builder/additional.js
Expand Up @@ -10,6 +10,46 @@ module.exports = function(knex) {

describe('Additional', function () {

it('should forward the .get() function from bluebird', function() {
return knex('accounts').select().limit(1).then(function(accounts){
var firstAccount = accounts[0];
return knex('accounts').select().limit(1).get(0).then(function(account){
expect(account.id == firstAccount.id);
});
});
});

it('should forward the .mapSeries() function from bluebird', function() {
var asyncTask = function(){
return new Promise(function(resolve, reject){
var output = asyncTask.num++;
setTimeout(function(){
resolve(output);
}, Math.random()*200);
});
};
asyncTask.num = 1;

var returnedValues = [];
return knex('accounts').select().limit(3).mapSeries(function(account){
return asyncTask().then(function(number){
returnedValues.push(number);
});
})
.then(function(){
expect(returnedValues[0] == 1);
expect(returnedValues[1] == 2);
expect(returnedValues[2] == 3);
});
});

it('should forward the .delay() function from bluebird', function() {
var startTime = (new Date()).valueOf();
return knex('accounts').select().limit(1).delay(300).then(function(accounts){
expect((new Date()).valueOf() - startTime > 300);
});
});

it('should truncate a table with truncate', function() {

return knex('test_table_two')
Expand Down