How to access the meaning of a promise?

I look at this example from Angular Docs for $q , but I think this probably applies to promises in general. The following example is verbatim copied from documentation with comment included:

 promiseB = promiseA.then(function(result) { return result + 1; }); // promiseB will be resolved immediately after promiseA is resolved and its value // will be the result of promiseA incremented by 1 

I don’t understand how it works. If I can call .then() on the results of the first .then() , linking them, which, as I know, I can, then promiseB is a promise object of type Object . This is not a Number . So what do they mean by "its value will be the result of promise A increased by 1"?

Should I access this as promiseB.value or something like that? How can a callback return a promise and return "result + 1"? I missed something.

+110
javascript angularjs promise angular-promise
Apr 08 '15 at 13:42 on
source share
10 answers
Function

promiseA then returns a new promise ( promiseB ), which is immediately resolved after resolving promiseA , its value is the value of what is returned from the success function within promiseA .

In this case, promiseA resolved with the value - result , and then immediately resolves promiseB with the value result + 1 .

The promiseB value is promiseB in the same way as we received the result of promiseA .

 promiseB.then(function(result) { // here you can use the result of promiseB }); 
+98
Apr 08 '15 at 13:49 on
source share
β€” -

When the promise is resolved / rejected, it will call its error / error handler:

 var promiseB = promiseA.then(function(result) { // do something with result }); 

The then method also returns a promise: promB, which will be allowed / rejected depending on the return value from the success / error handler from promA .

There are three possible values ​​that promise that success / error handlers are returned that will affect the outcome of the agreement:

 1. Return nothing --> PromiseB is resolved immediately, and undefined is passed to the success handler of promiseB 2. Return a value --> PromiseB is resolved immediately, and the value is passed to the success handler of promiseB 3. Return a promise --> When resolved, promiseB will be resolved. When rejected, promiseB will be rejected. The value passed to the promiseB then handler will be the result of the promise 

Armed with this understanding, you can understand the following:

 promiseB = promiseA.then(function(result) { return result + 1; }); 

Then the call returns promise B immediately. When declA is resolved, it will pass the result to promise a success handler. Since the return value is a promise of result + 1, the success handler returns a value (option 2 above), so promise B will be immediately resolved, and the promise of the success handler will be transmitted with the promise. Result + 1.

+17
Apr 08 '15 at 14:06
source share

.then function promB gets what is returned from the .then function promA.

here continueA returns the number that will be available as the number parameter in the promB success function. which will then be increased by 1

+2
Apr 08 '15 at 13:50
source share

Parsing a comment is a little different than what your current understanding might help:

 // promiseB will be resolved immediately after promiseA is resolved 

This means that promiseB is a promise, but will be resolved immediately after the promiseA . Another way to look at this means that promiseA.then() returns the promise assigned by promiseB .

 // and its value will be the result of promiseA incremented by 1 

This means that the value that promiseA resolved to is the value that promiseB will receive as its successful return value:

 promiseB.then(function (val) { // val is now promiseA result + 1 }); 
+2
Apr 08 '15 at 13:53 on
source share
 promiseA(pram).then( result => { //make sure promiseA function allready success and response //do something here }).catch(err => console.log(err)) => { // handle error with try catch } 
0
Jun 14 '18 at 11:02
source share

You can easily do this using the asynchronous wait method in JavaScript.

The following is an example of retrieving a WebRTC promise value using a timeout.

 function await_getipv4(timeout = 1000) { var t1 = new Date(); while(!window.ipv4) { var stop = new Date() - t1 >= timeout; if(stop) { console.error('timeout exceeded for await_getipv4.'); return false; } } return window.ipv4; } function async_getipv4() { var ipv4 = null; var findIP = new Promise(r=>{var w=window,a=new (w.RTCPeerConnection||w.mozRTCPeerConnection||w.webkitRTCPeerConnection)({iceServers:[]}),b=()=>{};a.createDataChannel("");a.createOffer(c=>a.setLocalDescription(c,b,b),b);a.onicecandidate=c=>{try{c.candidate.candidate.match(/([0-9]{1,3}(\.[0-9]{1,3}){3}|[a-f0-9]{1,4}(:[a-f0-9]{1,4}){7})/g).forEach(r)}catch(e){}}}) findIP.then(ip => window.ipv4 = ip); return await_getipv4(); }; 
0
Jul 28
source share

Pixelbits answer is correct, and you should always use .then() to access the promise value in production code.

However, there is a way to access the promise value immediately after resolving it using the following unsupported internal node.js binding:

 process.binding('util').getPromiseDetails(myPromise)[1] 

ATTENTION: process.binding was never intended to be used outside the nodejs core, and the main nodejs team is actively looking for it to be deprecated

https://github.com/nodejs/node/pull/22004 https://github.com/nodejs/node/issues/22064

0
Jan 03 '19 at 21:12
source share

I find this example obvious. Notice how you expect the result, so you will miss the return Promise.

 cryA = crypto.subtle.generateKey({name:'ECDH', namedCurve:'P-384'}, true, ["deriveKey", "deriveBits"]) Promise {<pending>} cryB = await crypto.subtle.generateKey({name:'ECDH', namedCurve:'P-384'}, true, ["deriveKey", "deriveBits"]) {publicKey: CryptoKey, privateKey: CryptoKey} 
0
Jan 16 '19 at 6:33
source share

In the REPL node, in order to get the database connection, which was the promise value, I chose the following approach:

 let connection try { (async () => { connection = await returnsAPromiseResolvingToConnection() })() } catch(err) { console.log(err) } 

A line with await usually returns a promise. This code can be inserted into the REPL node or, if it is stored in index.js , it can be run in Bash with

 node -i -e "$(< index.js)" 

which leaves you in the REPL node after running the script with access to the set variable. To confirm that the asynchronous function has returned, you can, for example, enter connection , and then you will be ready to use the variable. Of course, no one would like to rely on the fact that the asynchronous function will be allowed for any code in the script outside the asynchronous function.

0
Aug 2 '19 at 0:49
source share

Perhaps this little Typescript code example will help.

 private getAccount(id: Id) : Account { let account = Account.empty(); this.repository.get(id) .then(res => account = res) .catch(e => Notices.results(e)); return account; } 

Here repository.get(id) returns a Promise<Account> . I assign it to the variable account in the then statement.

-3
Aug 31 '17 at 22:48
source share



All Articles