1/*
2 A Promise is a proxy for a value not necessarily known when the promise is created.
3 It allows you to associate handlers with an asynchronous action's eventual success
4 value or failure reason.
5*/
6let promise = new Promise((resolve , reject) => {
7 fetch("https://myAPI")
8 .then((res) => {
9 // successfully got data
10 resolve(res);
11 })
12 .catch((err) => {
13 // an error occured
14 reject(err);
15 });
16});
1function doSomething() {
2 return new Promise((resolve, reject) => {
3 console.log("It is done.");
4 // Succeed half of the time.
5 if (Math.random() > .5) {
6 resolve("SUCCESS")
7 } else {
8 reject("FAILURE")
9 }
10 })
11}
12
13const promise = doSomething();
14promise.then(successCallback, failureCallback);
1We use promise to make a AsyncFunction, cose simetime we have to wait that function give
2us some result.
3Example, if we use ajax we have await ajax data or statament.
4_____________________________________
5Make a simple example.
6_____________________________________
7var asyncronus_function= (number)=>
8 {
9 return new Promise( (accept, reject)=>
10 {
11 })
12 }
13_____________________________________
14this function return a promise Object, thet required a function executor
15this functions (accept, reject) are defined in the executor
16function, that was needed in promise constructor.
17Syntax: new Promise (executor)
18executor= (accept, reject) =>{}
19
20if function end well we return a accept(), otherwise reject()
21_____________________________________
22Let complete asyncronus_function
23_____________________________________
24var asyncronus_function= (number)=>
25 {
26 return new Promise( (accept, reject)=>
27 {
28 if(number>10)
29 return accept("my first async");
30 return reject("my first async error")
31 })
32
33 }
34if it dont return any of this 2 function, Promise state is [PENDING] ,
35if return accept is [RESOLVED] end if return reject is [REJECTED]
36_____________________________________
37how we can retrieve accept or reject?
38_____________________________________
39there is two methods really important, that we have to consider afther we call this function
401) .then(function(error){}) is call when promise state is [RESOLVED]
412) .error(function(error){}) is call when promise state is [REJECTED]
423) do nothing if [PENDING]
43_____________________________________
44let call asyncronus_function()!!!
45_____________________________________
46 asyncronus_function(MY_NUMBER).then(function(data)
47 {
48 console.log(data)
49 }).catch(error =>
50 {
51 console.log(error)
52 });
53
54if MY_NUMBER> 10 ,asyncronus_function print data : OUTPUT my first async
55if MY_NUMBER<10 , asyncronus_function print error : OUTPUT my first async error
56HOPE it halp and have a nice day!
57
58
59
1A promise is an object that may produce a single value some time in the future:
2either a resolved value, or a reason that it’s not resolved
1A Promise is in one of these states:
2
3pending: initial state, neither fulfilled nor rejected.
4fulfilled: meaning that the operation was completed successfully.
5rejected: meaning that the operation failed.
1//example
2function tetheredGetNumber(resolve, reject) {
3 try {
4 setTimeout(
5 function() {
6 const randomInt = Date.now();
7 const value = randomInt % 10;
8 try {
9 if(value >= THRESHOLD_A) {
10 throw new Error(`Too large: ${value}`);
11 }
12 } catch(msg) {
13 reject(`Error in callback ${msg}`);
14 }
15 resolve(value);
16 return;
17 }, 500);
18 } catch(err) {
19 reject(`Error during setup: ${err}`);
20 }
21 return;
22}