Skip to content

Instantly share code, notes, and snippets.

@kavitshah8
Last active August 29, 2015 14:24
Show Gist options
  • Save kavitshah8/5fee316c12dc2809a5cd to your computer and use it in GitHub Desktop.
Save kavitshah8/5fee316c12dc2809a5cd to your computer and use it in GitHub Desktop.
Promise Fun
// Examples you can try in the Dev tools.
// Aim for these examples to demonstrate different ways of using promises
var p = Promise.resolve().then(function(){
console.log('Promise resolve callback');
}, function(){
console.log('Promise reject callback');
});
var p = Promise.reject().then(function(){
console.log('Promise resolve callback');
}, function(){
console.log('Promise reject callback');
});
Promise.reject()
.then(function() {
console.log('resolve1');
}, function() {
console.error('reject1');
})
.catch(function() {
console.error('catch');
});
-----
Promise.reject()
.then(function () {
console.log('resolve1');
})
.then(function () {
console.log('resolve2');
}, function () {
console.error('reject2');
})
.catch(function () {
console.error('catch');
})
-----
Promise.reject()
.then(function () {
console.log('resolve1');
})
.then(function () {
console.log('resolve2');
}, function () {
console.error('reject2');
})
.then(function () {
console.log('resolve3');
})
.catch(function () {
console.error('catch');
});
-----
Promise.reject()
.then(function () {
console.log('resolve1');
})
.then(function () {
console.log('resolve2');
})
.then(function () {
console.log('resolve3');
})
.catch(function () {
console.log('catch');
})
-----
var p = Promise.reject()
.then(function () {
console.log('resolve1');
})
.then(function () {
console.log('resolve2');
})
.then(function () {
console.log('resolve3');
})
.catch(function () {
console.log('catch');
});
p
-----
Promise.reject()
.then(function () {
console.log('resolve1');
}, function () {
console.log('reject1');
})
.then(function () {
console.log('resolve2');
}, function () {
console.log('reject2');
})
.catch(function () {
console.log('catch');
})
-----
Promise.reject()
.then(function () {
console.log('resolve1');
})
.then(function () {
console.log('resolve2');
})
.then(function () {
console.log('resolve3');
})
.catch(function () {
console.log('catch');
})
function getPromise() {
return Promise.resolve(1);
}
function logError() {
console.log(arguments);
}
function process () {
return Promise.reject('Promise Rejected');
//throw new Error('JS Error');
}
getPromise()
.then(function (result) {
return process(result);
})
.then(null, logError);
Promise.resolve(1).then(function () {
console.log(arguments);
});
// ---
Promise.resolve(1).then(function(){
console.log(arguments);
return 2;
}).then(function(){
console.log(arguments);
});
// ---
Promise.resolve().then(function () {
return 100;
})
.then(function() {
console.log(arguments);
});
// ---
Promise.resolve(200).then(function () {
console.log(arguments);
return Promise.resolve(100);
})
.then(function(arg1) {
console.log(arguments);
});
// ---
Promise.resolve().then(function () {
return 100;
}).then(function () {
console.log('1', arguments);
})
.then(function() {
console.log('2', arguments);
});
// ---
Promise.resolve().then(function () {
return 100;
})
.then(null)
.then(null)
.then(null)
.then(function() {
console.log(arguments);
})
.then(null)
.then(function() {
console.log(arguments);
});
// ---
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment