-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathpromises-es6.js
47 lines (40 loc) · 1.2 KB
/
promises-es6.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
/*
* Promises
*
* Promises are a library for asynchronous programming.
* A Promise represents an operation that hasn't completed yet, but is expected in the future.
* Promises are used in many existing JavaScript libraries.
*
* Syntax
* new Promise(function(resolve, reject) { ... });
*
* References
* https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise
* https://github.com/lukehoban/es6features
*/
/*
* States of a promise
*
* pending: initial state, not fulfilled or rejected.
* fulfilled: meaning that the operation completed successfully.
* rejected: meaning that the operation failed.
*/
var fetchData = function(userid){
return new Promise(function(resolve, reject){
// async task
setTimeout(function(){
if(userid == 1)
resolve({message: "Data successfully returned"});
else
reject({error: "Document not found", status: 404})
}, 100);
});
}
fetchData(1).then(function(data){
console.log(data); // {message: "Data successfully returned"}
});
fetchData("invalid text").then(function(data){
console.log(data); // won't execute
}).catch(function(err){
console.log(err); // {error: "Document not found", status: 404}
});