Async nightmares, the pyramid of doom, nested callback hierarchy, control flow: If you are familiar with any of these terms, you should be familiar with JavaScript promises.
As web apps get more sophisticated â" and the use of JavaScript on the server continues to grow â" thereâs an increasing need for an advanced way to handle flow control. Sometimes you find yourself with callbacks within callbacks within callbacks within callbacks (within callbacks). Other times you may have complicated business logic tangling up your code. Whatever your conundrum, you can use promises to simplify your code and make it more resilient.
Promises (also called deferreds or, even more confusingly, futures) is an approach in JavaScript where a flow is passed off to an object that is eventually either rejected (fail) or resolved (success). Any code waiting on a promise can then proceed based on the outcome.
The technique has been evolving over the last few years, and several libraries have been introduced. jQuery implemented Promises with version 1.5 (Jan. 2011), but most experts would advise you to use a library like Q, RSVP.js or when.js, even if you are already using jQuery. (jQueryâs implementation is not optimal.)
Domenic Denicola, one of the contributors to the Promises/A+ spec, visited The Times to discuss his work, and weâd like to share his talk with you.
.nytVideo, .nytVideo video, .youtubeVideo, .youtubeVideo iframe { background: none repeat scroll 0 0 #000000; } .youtubeVideo { position: relative; } .youtubeVideo, .youtubeVideo .thumb { height: 0; padding-bottom: 56.25%; } .youtubeVideo iframe { height: 100%; left: 0; position: absolute; top: 0; width: 100%; z-index: 1; } .youtubeVideo .playButton { border-radius: 10px 10px 10px 10px; height: 46px; left: 50%; margin: -23px 0 0 -35px; position: absolute; top: 50%; width: 70px; z-index: 2; background-image: -webkit-linear-gradient(90deg, #6e0610, #ff0000); /* Safari 5.1+, Mobile Safari, Chrome 10+ */ background-image: -moz-linear-gradient(90deg, #6e0610, #ff0000); /* Firefox 3.6+ */ background-image: -ms-linear-gradient(90deg, #6e0610, #ff0000); /* IE 10+ */ background-image: -o-linear-gradient(90deg, #6e0610, #ff0000); /* Opera 11.10+ */ } .youtubeVideo .thumb { overflow: hidden; width: 100%; } .youtubeVideo .thumb img { height: auto; width: 100%; margin-top:-45px; } .youtubeVideo:hover .thumb { cursor: pointer; } .youtubeVideo:hover .playButton { } .youtubeVideo .playButton .arrow { border-bottom: 10px solid transparent; border-left: 20px solid #FFFFFF; border-top: 10px solid transparent; height: 0; left: 28px; position: absolute; top: 13px; width: 0; } .clearfix:after { clear: both; content: "."; display: block; height: 0; visibility: hidden; }

In addition to being super smart, Denicola is also a really nice guy. He spends his days working at Lab49, and you can often catch him speaking at your favorite JavaScript conference. (At the recent HTML5Dev Conf, he gave a rousing rebuttal of RequireJS in favor of CommonJS.)
No comments:
Post a Comment