Skip to content

Instantly share code, notes, and snippets.

@jdudek jdudek/deferred.html
Created Mar 29, 2011

Embed
What would you like to do?
<!DOCTYPE html>
<html>
<head>
<script src="jquery.js" type="text/javascript"></script>
<script src="deferred.js" type="text/javascript"></script>
</head>
<body></body>
</html>
$(function () {
var runApp = $.Deferred()
var initFacebook = $.Deferred(function (dfr) {
runApp.done(function () {
setTimeout(function () {
console.log("initFacebook")
dfr.resolve()
}, 1500)
})
}).promise()
var initAudioPlayer = $.Deferred(function (dfr) {
runApp.done(function () {
setTimeout(function () {
console.log("initAudioPlayer")
dfr.resolve()
}, 5000)
})
}).promise()
var checkLike = function () {
return $.Deferred(function (dfr) {
setTimeout(function () {
console.log("checkLike")
// dfr.resolve()
dfr.reject()
}, 1000)
}).promise()
}
var askForLike = function () {
return $.Deferred(function (dfr) {
console.log("askForLike")
if (confirm("Like?")) {
dfr.resolve()
} else {
dfr.reject()
}
}).promise()
}
var requireLike = $.Deferred(function (dfr) {
initFacebook.done(function () {
checkLike().done(dfr.resolve).fail(function () {
askForLike().done(dfr.resolve).fail(dfr.reject)
})
})
}).promise()
var startGame = function () {
console.log("startGame")
}
$.when(initAudioPlayer, requireLike).then(startGame)
runApp.resolve()
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.