-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreact-native-promises-examples.js
63 lines (46 loc) · 1.26 KB
/
react-native-promises-examples.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
**Example1**
var promise = new Promise(function(resolve, reject) {
// do a thing, possibly async, then…
if (// worked) {
resolve("Sucessfully completed");
}
else {
reject("something went wrong"));
}
});
--------------------------------
**Example 2**
Also chanining can be used,
if used in a synchronous code, you want to do one after another.
getData('data.json').then(function(mydata) {
return getData(mydata[0]);
}).then(function(mydata0) {
console.log("Got mydata!", mydata);
})
--------------------------------
**Example 3**
Promises with Error functions.
Two functions inside the then(),
one for success and other for failure.
getData('data.json').then(function(response) {
console.log("Success!", response);
}, function(error) {
console.log("Failed!", error);
})
--------------------------------
**Example 4**
Promises with catch
getData('data.json').then(function(response) {
console.log("Success!", response);
}).catch(function(error) {
console.log("Failed!", error);
})
--------------------------------
**Example 5**
All Promises at once
What if we have a chain of functions and
we want to process it only when all completes.
Then the code will be like this.
Promise.all(arrayOfPromises).then(function(arrayOfResults) {
//...
})