Last active
October 22, 2015 17:50
-
-
Save aray12/f1603a9f7a6362d938f3 to your computer and use it in GitHub Desktop.
Promise.join with arrow function
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* This does not work on node v4.2.0 | |
* | |
* Reason has been determined. The first example of arrow functions is a function | |
* definition, not a function call. The second is a function call that passes the | |
* return value to Promise.join | |
*/ | |
'use strict'; | |
const Promise = require('bluebird'); | |
const models = require('../database'); | |
const resource = { | |
browse: function browse() { | |
return Promise.join( | |
() => models.table_one.findAll(), | |
() => models.table_two.findAll(), | |
() => models.table_three.findAll(), | |
function(table_one, table_two, table_three) { | |
const response = { | |
table_one, | |
table_two, | |
table_three, | |
}; | |
return response; | |
}); | |
}, | |
}; | |
module.exports = resource; | |
/** | |
* But this does | |
*/ | |
'use strict'; | |
const Promise = require('bluebird'); | |
const models = require('../database'); | |
const resource = { | |
browse: function browse() { | |
function getTableOne() { | |
return models.table_one.findAll(); | |
} | |
function getTableTwo() { | |
return models.table_two.findAll(); | |
} | |
function getTableThree() { | |
return models.table_three.findAll(); | |
} | |
return Promise.join( | |
getTableOne(), | |
getTableTwo(), | |
getTableThree(), | |
function(table_one, table_two, table_three) { | |
const response = { | |
table_one, | |
table_two, | |
table_three, | |
}; | |
return response; | |
}); | |
}, | |
}; | |
module.exports = resource; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment