Last active
August 29, 2015 14:24
-
-
Save kavitshah8/5fee316c12dc2809a5cd to your computer and use it in GitHub Desktop.
Promise Fun
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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'); | |
}); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
----- |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | |
}) | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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