Skip to content

Instantly share code, notes, and snippets.

Last active August 29, 2015 14:23
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
Promises chaining that solves "callbacks hell"
'use strict';
var Promise = require('promise');
function build() {
return new Promise(function(fulfill, reject) {
build().then(function(result) {
console.log(result); // `zero`
return 'one';
}).then(function(result) { // `one`
return ['apple', 'banana', 'pear'];
}).then(function(result) {
console.log(result); // `['apple', 'banana', 'pear']`
// Returning anything inside promise fulfillment/rejection
// callbacks, creates a promise for you automatically
// offering executions chaining.
// This is exactly how promises are approaching to
// solve so popular "callbacks hell".
return {
name: 'Nik Sumeiko',
website: ''
}).then(function(result) {
console.log(result); // `{ name: 'Nik Sumeiko', website: '' }`
return build();
}).then(function(result) {
console.log(result); // `zero`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment