Get the value of a promise and assign to variable

前端 未结 3 1307
轮回少年
轮回少年 2021-02-04 14:04

utility.fetchInfo() returns a Promise object. I need to be able to get the value of this Promise object and assign the value of it to a variable that I can then use

3条回答
  •  挽巷
    挽巷 (楼主)
    2021-02-04 14:30

    What @dhilt said but do your stuff inside the promise callback function:

    utility.fetchInfo().then(result => { 
      doSomethingWith(result);
    });
    

    Or use async/await if you are using a recent version of Node or Babel:

    async function myFunction () {
        var myVal = await utility.fetchInfo();
    }
    

提交回复
热议问题