Assign value from successful promise resolve to external variable

前端 未结 5 1639
陌清茗
陌清茗 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.

提交回复
热议问题