Last active
May 21, 2018 15:14
-
-
Save abhi11210646/c51950fccce71d07ce883636b3c934a1 to your computer and use it in GitHub Desktop.
Promise methods like all. race, reject, resolve etc. And Async/Await functionality
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
/* | |
Async: -"When an async function is called, it returns a Promise. When the async function returns a value, | |
the Promise will be resolved with the returned value.When the async function throws an exception or some value, | |
the Promise will be rejected with the thrown value.An async function can contain an await expression, | |
that pauses the execution of the async function and waits for the passed Promise's resolution, | |
and then resumes the async function's execution and returns the resolved value." | |
Await:- "The await expression causes async function execution to pause until a Promise is fulfilled or rejected, | |
and to resume execution of the async function after fulfillment. When resumed, the value of the await expression is | |
that of the fulfilled Promise.If the Promise is rejected, the await expression throws the rejected value. | |
If the value of the expression following the await operator is not a Promise, it's converted to a resolved Promise." | |
*/ | |
/* Start of basic async await functionality */ | |
// asyncCallerFunction will return new Promise. | |
function asyncCallerFunction(){ | |
return new Promise((resolve, reject)=>{ | |
setTimeout(()=>{resolve('success')},1000); | |
}); | |
} | |
// this function call asyncCallerFunction and return the PromiseValue. | |
async function asyncFunction() { | |
return await asyncCallerFunction(); | |
} | |
asyncFunction().then(x=>console.log('success response=',x)).catch(e=>console.log('error response',e)); | |
// output:- "success response='success'" | |
/* End of basic async await functionality */ |
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
/* | |
The Promise.all() method returns a single Promise that resolves when all of the promises in the iterable argument | |
have resolved or when the iterable argument contains no promises. It rejects with the reason of the first promise that rejects. | |
The Promise.reject(reason) method returns a Promise object that is rejected with the given reason. | |
The Promise.resolve(value) method returns a Promise object that is resolved with the given value. | |
If the value is a thenable (i.e. has a "then" method), the returned promise will "follow" that thenable, | |
adopting its eventual state; if the value was a promise, that object becomes the result of the call to Promise.resolve; | |
otherwise the returned promise will be fulfilled with the value. | |
*/ | |
// Example 1:- | |
var p1 = Promise.resolve(3); | |
var p2 = 1337; | |
Promise.all([p1, p2]).then(values => { | |
console.log(values); // [3, 1337] | |
}); | |
// Example 2:- | |
var p1 = Promise.resolve(3); | |
var p2 = Promise.reject('I am about to rejected'); | |
Promise.all([p1, p2]).then(values => { | |
console.log(values); // No output here... catch block will be called due to one Promise rejection. | |
}).catch((error)=>{ | |
console.log(error); // 'I am about to rejected' | |
}); | |
/* | |
The Promise.race(iterable) method returns a promise that resolves or rejects as soon as one of the promises in the iterable | |
resolves or rejects, with the value or reason from that promise. | |
*/ | |
// Example 3:- | |
Promise.race([]).then(x=>console.log('x',x)).catch(e=>console.log('e',e)) | |
//Output:- "no output" as Promise {<pending>} | |
Promise.race([1,2,3,4]).then(x=>console.log('response=',x)).catch(e=>console.log('e',e)) | |
//Output:- "response=1" as Promise {<resolved>: undefined} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment