Assign value from successful promise resolve to external variable

前端 未结 5 1637
陌清茗
陌清茗 2020-12-23 19:23

I have a pretty silly problem. Consider the following:

vm.feed = getFeed().then(function(data) {return data;});

getFeed() retu

相关标签:
5条回答
  • 2020-12-23 20:02

    You could provide your function with the object and its attribute. Next, do what you need to do inside the function. Finally, assign the value returned in the promise to the right place in your object. Here's an example:

    let myFunction = function (vm, feed) {
        getFeed().then( data => {
            vm[feed] = data
        })
    } 
    
    myFunction(vm, "feed")
    

    You can also write a self-invoking function if you want.

    0 讨论(0)
  • 2020-12-23 20:10

    This could be updated to ES6 with arrow functions and look like:

    getFeed().then(data => vm.feed = data);
    

    If you wish to use the async function, you could also do like that:

    async function myFunction(){
        vm.feed = await getFeed();
        // do whatever you need with vm.feed below
     }
    
    0 讨论(0)
  • 2020-12-23 20:19

    The then() method returns a Promise. It takes two arguments, both are callback functions for the success and failure cases of the Promise. the promise object itself doesn't give you the resolved data directly, the interface of this object only provides the data via callbacks supplied. So, you have to do this like this:

    getFeed().then(function(data) { vm.feed = data;});
    

    The then() function returns the promise with a resolved value of the previous then() callback, allowing you the pass the value to subsequent callbacks:

    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
    
    0 讨论(0)
  • 2020-12-23 20:21

    Your statement does nothing more than ask the interpreter to assign the value returned from then() to the vm.feed variable. then() returns you a Promise (as you can see here: https://github.com/angular/angular.js/blob/master/src/ng/q.js#L283). You could picture this by seeing that the Promise (a simple object) is being pulled out of the function and getting assigned to vm.feed. This happens as soon as the interpreter executes that line.

    Since your successful callback does not run when you call then() but only when your promise gets resolved (at a later time, asynchronously) it would be impossible for then() to return its value for the caller. This is the default way Javascript works. This was the exact reason Promises were introduced, so you could ask the interpreter to push the value to you, in the form of a callback.

    Though on a future version that is being worked on for JavaScript (ES2016) a couple keywords will be introduced to work pretty much as you are expecting right now. The good news is you can start writing code like this today through transpilation from ES2016 to the current widely supported version (ES5).

    A nice introduction to the topic is available at: https://www.youtube.com/watch?v=lil4YCCXRYc

    To use it right now you can transpile your code through Babel: https://babeljs.io/docs/usage/experimental/ (by running with --stage 1).

    You can also see some examples here: https://github.com/lukehoban/ecmascript-asyncawait.

    0 讨论(0)
  • 2020-12-23 20:24

    This is one "trick" you can do since your out of an async function so can't use await keywork

    Do what you want to do with vm.feed inside a setTimeout

    vm.feed = getFeed().then(function(data) {return data;});
    
     setTimeout(() => {
        // do you stuf here
        // after the time you promise will be revolved or rejected
        // if you need some of the values in here immediately out of settimeout
        // might occur an error if promise wore not yet resolved or rejected
        console.log("vm.feed",vm.feed);
     }, 100);
    
    0 讨论(0)
提交回复
热议问题