Created
March 25, 2014 20:31
-
-
Save gordienoye/9770699 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
<html> | |
<head> | |
<title>Monad Promises</title> | |
<script src="https://cdn.goinstant.net/v1/platform.min.js"></script> | |
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> | |
</head> | |
<body> | |
<script> | |
function dumpTheMessages(messages) { | |
for (messageId in messages) { | |
var message = messages[messageId]; | |
var displayElement = '<p>[' + message.timestamp + '] ' + | |
'<span style="color:red">' + message.user + ' says: </span>' + | |
message.text + '</p>'; | |
$('body').append(displayElement); | |
} | |
} | |
$(document).ready(function () { | |
var newMessage = { | |
text: 'A new message huzzah', | |
user: 'Monad Example', | |
timestamp: Date.now() | |
}; | |
var chatMessagesKey; | |
// TODO: replace this URL with your GoInstant application URL | |
var url = 'https://goinstant.net/<ACCOUNT>/<APPLICATION>'; | |
goinstant | |
// returns a monad | |
.connect(url) | |
// custom function that we ensure returns a monad with the result | |
// of a request to get the chat messages | |
.then(function(result) { | |
var lobby = result.rooms[0]; | |
chatMessagesKey = lobby.key('chatMessages'); | |
return chatMessagesKey.get(); | |
}) | |
.then(function(result) { | |
dumpTheMessages(result.value); | |
return chatMessagesKey.add(newMessage); | |
}) | |
// custom function that we ensure returns a monad | |
.then(function() { | |
// dividing line to see the difference | |
$('body').append('<hr>'); | |
return chatMessagesKey.get(); | |
}) | |
.then(function(result) { | |
dumpTheMessages(result.value); | |
}) | |
.catch(function(err) { | |
throw "Somthing bad happened"; | |
}); | |
}); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment