Redemption from Callback Hell

InfoQ · 38K views · 2013 · 50:25

Open on YouTube

Web developmentPython, Ruby & scripting languagesSoftware architecture & designtalk

Why Stack Overflow recommends it

Stack Overflow users link it as the go-to explanation of how promises work and why chaining them beats nested callbacks, often when answering or asking about promise behavior.

Recommended by 2 people across 2 different months, 2011 to 2016.

What it is

A 2013 conference talk from InfoQ in which Michael Jackson and Domenic Denicola explain how JavaScript developers end up in callback hell and how Promises (including Promises/A+) and fully asynchronous APIs help avoid it. It is conceptual rather than a single narrow example, and comes with slides.

What people said

Strong rec reza · 2013-09 · Stack Overflow · on “node.js and single page web application”

whoa, thank you so much! the time you took to explain it soooo well. Thank you, it does help for sure. BTW, just yesterday I was watching a great presentation of promise/A+. Highly useful [this video]

Recommends Camilo Martin · 2012-11 · Stack Overflow · on “What are the differences between Deferred, Promise and Future in JavaScript?”

What really made it all click for me was this presentation [LINK] by Domenic Denicola. In a github gist [LINK] , he gave the description I like most, it's very concise: The point of promises is to give us back functional composition and error bubbling in the async world. In other word, promises are a way that lets us write asynchronous code that is almost as easy to write as if it was synchronous . Consider this example, with promises: getTweetsFor("domenic") // promise-returning async function .then(function (tweets) { var shortUrls = parseTweetsForUrls(tweets); var mostRecentShortUrl = shortUrls[0]; return expandUrlUsingTwitterApi(mostRecentShortUrl); // promise-returning async function })

Cites rsp · 2011-03 · Stack Overflow · on “jQuery: Return data after ajax call success”

Note: This answer was written in February 2010. See updates from 2015, 2016 and 2017 at the bottom. You can't return anything from a function that is asynchronous. What you can return is a promise . I explained how promises work in jQuery in my answers to those questions: JavaScript function that returns AJAX call data [LINK] jQuery jqXHR - cancel chained calls, trigger error chain [LINK] If you could explain why do you want to return the data and what do you want to do with it later, then I might be able to give you a more specific answer how to do it. Generally, instead of: function testAjax() { $.ajax({ url: "getvalue.php", success: function(data) { return data; } }); } you can write your

Cites keshav · 2015-12 · Stack Overflow · on “Bluebird promises and callbacks”

A callback is any function that is called by another function which takes the first function as a parameter. It is “called back” at some specified point inside that function. While we are talking about the javascript, we are dealing with callbacks all the time. You just cannot avoid callbacks, they are at the core of node js library. They enable a balanced, non-blocking flow of control across applications, they are the foundation of almost all promise libraries. In your code, function passed to then is a callback function only. Promises are effectively a different style for achieving the same effect as callbacks. The advantage here is an abstraction that makes working with callbacks extremel

Cites BugHunterUK · 2016-04 · Stack Overflow · on “When using promises why is the last then called?”

I'm watching this video [this video] (code around 36 minutes) on Promises, but I'm confused by a bit of code: getUser('mjackson') .then(getTweets) .then(updateStatus) .then(undefined, handleError); My problem with understand this is why is the last then called? And why does it pass undefined as the first argument? If getTweets() fails, then updateStatus() is never called. Which is why I'm confused as to why the last then is called if the second (the one that contains updateStatus() ) isn't. I know handleError is a callback, I just don't get why undefined is passed. I hope that makes sense.

Cites lilbiscuit · 2016-08 · Stack Overflow · on “Angular .then undefined”

I am trying to implement this promise pattern [this video] in an Angularjs app. But someUserContent returns the value true not the actual data as desired. How do I get the data to the controller? In my controller: .controller('MyCtrl',function() { MyFactory.checkUser(userId) //returns true .then(MyFactory.prepDatabase()) //returns true .then(Myfactory.getUserContent()) //returns a someUserData .then(function(someUserData) { //do some stuff with all this data }); } And the factory: .factory('MyFactory', function($q,$http....) { MyFactory.prototype.checkUser = function(user_id) { var self = this; this.db = new PouchDB('users', {location: 'default'}); return $q.when(true); } Myfactory.prototype.getU