Assign value from successful promise resolve to external variable

前端 未结 5 1647
陌清茗
陌清茗 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: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);
    

提交回复
热议问题