Created
October 14, 2010 21:15
-
-
Save bentruyman/627064 to your computer and use it in GitHub Desktop.
Promise Example
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
// npm install promised-io | |
var promised = require('promised-io/promise'); | |
// all of these async methods return an instance of promised.Promise | |
var filePromise = getFile('/foo.html') | |
, databasePromise = getDatabaseRecord('users/bentruyman') | |
, websocketPromise = establishWebsocketConnection(); | |
promised.all([filePromise, databasePromise, websocketPromise], function (arrayOfResolvedData) { | |
// all promises have been resolved | |
}); | |
function getFile(filename) { | |
var promise = new promised.Promise(); | |
getFileFromSomewhereAsynchronously(filename, function (data) { | |
promise.resolve(data); | |
}); | |
return promise; | |
} | |
// etc... |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment