Created
June 2, 2015 23:07
-
-
Save XeeD/f287ec35c2ca9d326856 to your computer and use it in GitHub Desktop.
Este user state for multiple promises
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
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} | |
} |
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
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