Skip to content

Instantly share code, notes, and snippets.

@XeeD
Created June 2, 2015 23:07
Show Gist options
  • Save XeeD/f287ec35c2ca9d326856 to your computer and use it in GitHub Desktop.
Save XeeD/f287ec35c2ca9d326856 to your computer and use it in GitHub Desktop.
Este user state for multiple promises
import Promise from 'bluebird'
import config from '../config'
export default function loadUserState(req, res, next) {
const dataSources = [loadMessages(), loadSomeOtherData()]
if (config.translationServer.loadTranslations)
dataSources.push(loadTranslations())
Promise.settle(dataSources).then(receivedData => {
req.userState = receivedData
.filter(promise => promise.isFulfilled())
.map(promise => promise.value())
next()
})
}
function loadTranslations() {
const request = fetch(`${config.translationServer.url}/translations`, {
method: 'get',
credentials: 'include',
headers: {
'Authorization': `Token token=${config.translationServer.apiKey}`,
'Content-type': 'application/json'
}
})
return Promise.resolve(request)
.then(response => response.ok ? response.json() : {})
.then(i18nData => {
return {i18n: i18nData}
})
}
function loadFlashMessages() {
// ...
return {flashMessages}
}
export function appStateForUser(req) {
const {userState} = req
return Immutable.fromJS(initialState).mergeDeep(...userState).toJS()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment