We explore JavaScript promise chains with multiple values within a stateful context.
it('should display the status of B in A\'s timeline when A follows B', function() {
return Promise.all([createUser('foo'), createUser('bar')])
.then(function([user1, user2]) {
return user1.follow(user2)
.then(function() {
return user2.updateStatus('My status')
})
.then(function(status) {
return user1.getTimeline()
.then(function(timeline) {
expectTimelineToContainStatus(timeline, status)
})
})
})
})
it('should display the status of B in A\'s timeline when A follows B', function() {
return Promise.all([createUser('foo'), createUser('bar')])
.then(function([user1, user2]) {
return Promise.all([user1, user2, user1.follow(user2)])
})
.then(function([user1, user2]) {
return Promise.all([user1, user2.updateStatus('My status')])
})
.then(function([user1, status]) {
return Promise.all([user1.getTimeline(), status])
})
.then(function([timeline, status]) {
expectTimelineToContainStatus(timeline, status)
})
})
it('should display the status of B in A\'s timeline when A follows B', function() {
var user1, user2, status
return Promise.all([createUser('foo'), createUser('bar')])
.then(function([_user1, _user2]) {
user1 = _user1
user2 = _user2
return user1.follow(user2)
})
.then(function() {
return user2.updateStatus('My status')
})
.then(function(_status) {
status = _status
return user1.getTimeline()
})
.then(function(timeline) {
expectTimelineToContainStatus(timeline, status)
})
})
it('should display the status of B in A\'s timeline when A follows B', function() {
var the = { }
return Promise.all([createUser('foo'), createUser('bar')])
.then(function([user1, user2]) {
the.firstUser = user1
the.secondUser = user2
return the.firstUser.follow(the.secondUser)
})
.then(function() {
return the.secondUser.updateStatus('My status')
})
.then(function(status) {
the.status = status
return the.firstUser.getTimeline()
})
.then(function(timeline) {
expectTimelineToContainStatus(timeline, the.status)
})
})
it('should display the status of B in A\'s timeline when A follows B', function() {
var the = { }
return set(the, {
firstUser: createUser('foo'),
secondUser: createUser('bar')
})
.then(function() {
return the.firstUser.follow(the.secondUser)
})
.then(function() {
return set(the, { status: the.secondUser.updateStatus('My status') })
})
.then(function() {
return set(the, { timeline: the.firstUser.getTimeline() })
})
.then(function() {
expectTimelineToContainStatus(the.timeline, the.status)
})
})
var the
beforeEach(function() { the = { } })
describe('following another user', function() {
beforeEach(function() {
return set(the, {
firstUser: createUser('foo'),
secondUser: createUser('bar')
})
.then(function() {
return the.firstUser.follow(the.secondUser)
})
.then(function() {
return set(the, { status: the.secondUser.updateStatus('My status') })
})
})
it('should display the status of B in A\'s timeline when A follows B', function() {
return the.firstUser.getTimeline().then(function(timeline) {
expectTimelineToContainStatus(the.timeline, the.status)
})
})
})
var keys = require('when/keys')
function set(context, promises) {
return keys.all(promises).then(function(results) {
for (var key in results) {
context[key] = results[key]
}
})
}
function set(context, promises) {
return keys.all(promises).then(function(results) {
Object.assign(context, results)
})
}
function set(context, promises) {
return keys.all(promises).then(Object.assign.bind(null, context))
}
var set = require('stateful-context').set
var StatefulContext = require('stateful-context')
var the, set
beforeEach(function() {
the = new StatefulContext()
set = the.set
})
describe('following another user', function() {
beforeEach(function() {
return set({
firstUser: createUser('foo'),
secondUser: createUser('bar')
})
.then(function() {
return the.firstUser.follow(the.secondUser)
})
})
// ...
})
var locals
beforeEach(function() {
locals = new StatefulContext()
})
describe('following another user', function() {
beforeEach(function() {
return locals.set({
firstUser: createUser('foo'),
secondUser: createUser('bar')
})
.then(function() {
return locals.firstUser.follow(locals.secondUser)
})
})
// ...
})
describe 'following another user', ->
declare
firstUser: -> createUser('foo')
secondUser: -> createUser('bar')
timeline: (firstUser) -> firstUser.getTimeline()
status: (secondUser) -> secondUser.updateStatus 'My status'
beforeEach ->
get (firstUser, secondUser) -> firstUser.follow secondUser
it 'should display the status of B in A\'s timeline when A follows B', ->
get (timeline, status) -> expectTimelineToContainStatus timeline, status
From us to your inbox weekly.