简体   繁体   English

承诺回报价值

[英]Return Value in Promise

I know this questions is asked several times in several ways. 我知道这个问题以几种方式被问过几次。 But I realy dont get it: 但我真的不明白这一点:

I have a promise with a resolved value. 我有一个坚定的承诺。 I can console.log this object and everything seems to be fine. 我可以console.log这个对象,一切似乎都很好。 I can see, what I want to see. 我可以看到,我想看到的。

I use PouchDB and NuxtJS (VueJS) 我使用PouchDB和NuxtJS(VueJS)

import PouchDB from 'pouchdb'

let testdb = new PouchDB('testdb');

let testDoc = function () {
  testdb.get('2').then(function (doc) {
    console.log(doc);
  }).catch(function (err) {
    console.log(err);
  });
}

This works great. 这很好。 My result is what I expect and hope to see: 我的结果是我期望并希望看到的:

{
   text: "testen", 
   _id: "2", 
   _rev: "1-c7e7c73d264aa5e6ed6b5cc10df35c5a"
}

Perfect. 完善。 But now I am struggeling with returning this value, so other functions can access to it. 但是现在我在努力返回这个值,以便其他函数可以访问它。 Especially returning this data. 特别是返回此数据。 In VueJS eg like that: 在VueJS中,例如:

// ..
export default {
   data() {
      return {
         doc: testDoc
      }
   }
}

So I can access to it via instance. 所以我可以通过实例访问它。 But ofcourse, If I do it like that, data is promise 但是,当然,如果我那样做,数据是有希望的

data: [
   doc: promise
]

But I need the value, not what it is. 但是我需要的是价值,而不是价值。 I dont understand how to return the value. 我不明白如何返回值。

I have read several How To´s. 我读了几本《如何做》。 I guess, I understand the different between Callback and Promise. 我想,我了解Callback和Promise之间的区别。 With both and async functions I get the same result. 使用和异步函数,我得到相同的结果。 But all example are always with console.log(). 但是所有示例始终使用console.log()。 But this works for me. 但这对我有用。

Has anyone an example hot to access this (scoped or nested?) value? 有没有人喜欢访问此(作用域或嵌套?)值的示例?

If I return the data: 如果我返回数据:

let testdb = new PouchDB('testdb');

let testDoc = function () {
  testdb.get('2').then(function (doc) {
    return doc;
  }).catch(function (err) {
    console.log(err);
  });
}

Why hasnt testDoc the value now? 为什么现在没有testDoc的价值? Or where the hack is the value? 还是骇客的价值所在?

I always have done it via commiting the value into the vuex store. 我总是通过将值提交到vuex存储中来完成此操作。 This also works great. 这也很好。

let fetchOrga = async function({ store }) {
  try {
    let orgaDoc = await orgadb.get('orga');
    store.commit('orgaUpdate', orgaDoc)
  } catch (err) {
    console.log(err);
  }
}

But like I said, I want to have this data directly under control via IndexedDB 但是就像我说的,我想通过IndexedDB直接控制这些数据

You can use async/await to wait until promise resolve: 您可以使用async / await等待诺言解决:

// nuxt.js way
async asyncData() {
  let testdb = new PouchDB('testdb');
  return {
    doc: await testdb.get('2'),
  };
},

UPD (by comments): UPD(通过评论):

data() {
  return {
    isReady: false,
    doc: null,
  };
},
async mounted() {
  let testdb = new PouchDB('testdb');
  this.doc = await testdb.get('2');
  this.isReady = true;
},

In the mount of the component you should update your state doc then your doc will be available to work with anywhere in your inside your component. 在组件的安装中,您应该更新state doc然后您的文档将可以在组件内部的任何位置使用。

export default {
  data() {
    return {
      doc: [],
      error : ""
    }
  },
  mounted: () => {
    testdb.get('2').then(doc => {
      this.doc = doc;
    }).catch(function(err) {
      this.error = err;
    });
  }
} 

testDoc in your example now contains a function . 您的示例中的testDoc现在包含一个函数 Not a promise nor the doc you're getting from the promnise. 不承诺也不doc你从promnise获得。 To get the actual doc you need to get inside the promise . 要获得实际的doc您需要兑现承诺 Like this 像这样

let testdb = new PouchDB('testdb');

testdb.get('2').then(function (doc) {

    // Your doc is here
    console.log(doc);
    return doc;
}).catch(function (err) {
    console.log(err);
});

Alternatively you can use the async/await syntax, like this 另外,您可以使用async / await语法,如下所示

let doc = await testdb.get('2');
// Your doc is here
console.log(doc);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM