Last active
September 26, 2016 12:47
-
-
Save shospodarets/c1c245a39db81c073709 to your computer and use it in GitHub Desktop.
Description of the native JavaScript promises API with examples
This file contains 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
// Current browser support for native promises: http://kangax.github.io/compat-table/es6/#Promise | |
// Article: http://www.html5rocks.com/en/tutorials/es6/promises/ | |
// Polyfills: | |
// https://github.com/getify/native-promise-only | |
// https://github.com/jakearchibald/es6-promise | |
/*------------------------------ GET PROMISE ------------------------------*/ | |
// N.B.: Promises pass only first argument to resolve/reject callbacks, others are ignored | |
function doSomething(){ | |
return new Promise(function(resolve, reject) { | |
setTimeout(function(){ | |
if(Math.random()>0.5){// toBeOrNotToBe :) | |
resolve("promise resolved"); | |
}else{ | |
reject('promise rejected'); | |
} | |
}); | |
}); | |
} | |
//---------- Add "done" and "fail" actions ---------- | |
doSomething() | |
.then(function(res){ | |
console.log('done', res); | |
},function(res){ | |
console.log('fail', res); | |
}); | |
//---------- Add only "fail" action ---------- | |
doSomething() | |
.then(undefined, function(res){ | |
console.log('fail', res); | |
}); | |
//---------- Add "always" ---------- | |
// there isn't "finally" callback https://github.com/domenic/promises-unwrapping/issues/18 | |
doSomething() | |
.then(function(res){ | |
console.log('always', res); | |
},function(res){ | |
console.log('always', res); | |
}); | |
/*------------------------------ RESOLVE PROMISE OUTSIDE ------------------------------*/ | |
// but *...it's useful to do all your promise-related work inside the promise constructor callback* | |
function doSomething() { | |
var wrappedPromise = {}, | |
promise = new Promise(function (resolve, reject) { | |
wrappedPromise.resolve = resolve; | |
wrappedPromise.reject = reject; | |
}); | |
wrappedPromise.then = promise.then.bind(promise); | |
wrappedPromise.catch = promise.catch.bind(promise); | |
wrappedPromise.promise = promise;// e.g. if you want to provide somewhere only promise, without resolve/reject methods | |
return wrappedPromise; | |
} | |
var wrappedPromise = doSomething(); | |
wrappedPromise | |
.then(function (res) { | |
console.log('done', res); | |
}, function (res) { | |
console.log('fail', res); | |
}); | |
wrappedPromise.resolve('trigerred resolve'); | |
/*------------------------------ CATCH ------------------------------*/ | |
// N.B.: if an error is thrown in the constructor callback-> it triggers "reject" | |
doSomething() | |
.then(f1) | |
.then(f2) | |
.catch(r1) | |
.then(f3) | |
.catch(r2); | |
// is analogous to: | |
try { | |
try { | |
doSomething(); | |
f1(); | |
f2(); | |
} catch (e) { | |
r1(e); | |
} | |
f3(); | |
} catch (e) { | |
r2(e); | |
} | |
/*------------------------------ JQUERY DEFERRED TO NATIVE PROMISE ------------------------------*/ | |
var jQueryPromise = $.ajax('/data.json');// or $.Deferred(); | |
var nativePromise = Promise.resolve(jQueryPromise); | |
// or | |
var nativePromise = new Promise(function (resolve, reject) { | |
jQueryPromise.then(resolve, reject); | |
}); | |
/*------------------------------ GET RESOLVED/REJECTED PROMISES ------------------------------*/ | |
// RESOLVED | |
var resolvedPromise = Promise.resolve('Resolved'); | |
// or | |
var resolvedPromise = new Promise(function (resolve) { | |
resolve('Resolved'); | |
}); | |
// REJECTED | |
var rejectedPromise = Promise.reject(new Error('Rejected')); | |
// or | |
var rejectedPromise = new Promise(function (resolve, reject) { | |
reject(new Error('Rejected')); | |
}); | |
// N.B.: RESOLVE/REJECT ARE ALWAYS CALLED ASYNCHRONOUSLY | |
var test = 'Empty'; | |
resolvedPromise.then(function(res){ | |
test = 'Changed'; | |
}); | |
console.log(test);// 'Empty'; | |
setTimeout(function(){ | |
console.log(test);// 'Changed' | |
}, 0); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment