从nodejs中的promise函数获取值

时间:2017-09-21 22:34:00

标签: node.js

我在utilityfunction.js文件中有以下内容:

module.exports = {
 grabValue: function() {
  var params = {
    apiVersion: 'v1',
    endpoint: "https://localhost:8200",
    token: "MY_TOKEN"
  };

  var vault = require("node-vault")(params);
  vault.read('secret/mysecret/foo').then(v => {
     var myvalue = result.data.value;
     resolve(myvalue);
  }).catch(e => console.error(e));
 }
};

然后在另一个名为use.js的文件中,我导入utilityfunction.js,如下所示:

var rUtil = require('./utilityfunction.js'); 
var getMyValue = rUtil.grabValue();

这会为undefined返回getMyValue值。

我基本上想从我的grabValue函数中返回值。

2 个答案:

答案 0 :(得分:1)

您应该返回char** arr; arr = new char*[100]; // for loop that allocates the internal arrays 来电的结果。另一个错误是,你调用了未定义的vault.read函数,我猜你试图用构造函数创建一个resolve并保留它:

Promise

因为module.exports = { grabValue: function() { ... var vault = require("node-vault")(params); return vault.read('secret/mysecret/foo') .then(v => { var myvalue = result.data.value; return myvalue; }) .catch(err => console.error(err)); } }; 是异步函数,你不能立即得到结果,你需要使用grabValue回调:

then

答案 1 :(得分:1)

Promises基本上是一个带有.then.catch属性的构造函数,每个属性都将函数作为第一个参数。当Promise解析时,它将您作为参数传递的函数调用.then,如果Promise拒绝,它会将您作为参数传递的函数调用.catch

话虽这么说,当你想要检索由Promise解析的值时,你会将它作为传递给.then的函数的第一个参数。

示例:

// let's start a promise
new Promise((resolve, reject) => {
     // let's resolve a promise
     resolve('foo');
})
.then(result => {
     // if our promise is resolved, it will call the function here
     // result argument is the value passed to the resolution
     console.log(result); // foo
});

所以在你的情况下,应该重写grabValue:

grabValue: function() {
  var params = {
    apiVersion: 'v1',
    endpoint: "https://localhost:8200",
    token: "MY_TOKEN"
  };

  var vault = require("node-vault")(params);
  return vault.read('secret/mysecret/foo')
  .then(v => {
     return v;
  })
  .catch(console.error);
 }

然后调用grabValue:

grabValue()
.then(result => {
    console.log(result); // result is v
    // continue your logic here
});

承诺永远不会归还其他任何东西。 所以你不能做那样的事情:

const a = new Promise((resolve, reject) => {
    return resolve('foo');
});

// here a will be a Promise and the only way to access 'foo'
// is to call the `.then` this way :

a.then((result) => {
    // result = 'foo'
});

请注意,当在Promise链中(意味着在.then.catch回调中)时,如果您正在执行异步调用,则必须返回Promise以确保下一个.then将等待异步调用完成,但是如果你没有进行异步调用,你可以只返回一个值,它将是你回调的第一个参数。如果在Promise链中抛出错误,它将被.catch回调捕获。

示例:

const somePromise = new Promise((resolve, reject) => {
     return resolve();
})
.then(() => {
    // i'm doing some async
    // so i return a Promise
    // the next .then will be called only when my promise is resolved
    return new Promise((resolve, reject) => {
         resolve('foo');   
    });
})
.then((result) => {
    // I'm not doing any async i can just return the value
    return result + 'bar';
});

// then I can get the value from somePromise
somePromise.then((result) => {
     console.log(result); // foobar
     // I'm in a Promise chain and an error occurs
     throw new Error('Oups something went wrong');
})
.catch((err) => {
   console.log(err.message); // Oups something went wrong
});