typescript 如何使用angular 2返回promise的值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39124876/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to return value of promise using angular 2
提问by PAncho
This is my promise function i need to return the value of rs.rows.item(0);
这是我的承诺函数,我需要返回 rs.rows.item(0) 的值;
public getCustomer() : any
{
let db = window.sqlitePlugin.openDatabase({name: 'data.db', location: 'default'});
return new Promise((resolve, reject) =>
{
db.transaction(function(tx)
{
tx.executeSql('SELECT * FROM customer ORDER BY customerId DESC LIMIT 1', [], function(tx, rs)
{
return resolve(rs.rows.item(0));
},
function(tx, error)
{
console.log('SELECT error: ' + error.message);
reject(error);
});
});
});
}
the return value i got an object like this image
i need to get like this example
我需要像这个例子一样
var customer = getCustomer();
customer.name;
customer.email;
采纳答案by Volodymyr Khmil
first, you need func to get all your data:
首先,您需要 func 来获取所有数据:
getAll(): Promise<Phrase[]> {
return phrasesPromise;
}
second, if you need one item you can use
第二,如果你需要一件物品,你可以使用
ngOnInit() {
this.phraseService
.getAll()
.then((result: Phrase[]) => this.phrases = result);
}
回答by sebaferreras
Promisesprovide us with abstractions that help us deal with the asynchronous nature of our applications. Since we don't know how much time will those operations take (and therefore, when is the data going to be available) you need to use the then()
method to execute some code when the data is ready to be used:
Promise为我们提供了抽象,帮助我们处理应用程序的异步特性。由于我们不知道这些操作需要多长时间(因此,数据何时可用)您需要使用该then()
方法在数据准备好使用时执行一些代码:
this.getCustomer()
.then((data) => {
// Here you can use the data because it's ready
// this.myVariable = data;
})
.catch((ex) => {
console.log(ex);
});
回答by James Monger
This is a Promise
, so you need to use then
:
这是一个Promise
,所以你需要使用then
:
getCustomer()
.then(customer => {
customer.name;
customer.email;
});
If you are using TypeScript, or a version of JavaScript that supports async
/await
, you can do this:
如果您使用的是 TypeScript 或支持async
/的 JavaScript 版本await
,您可以这样做:
var customer = await getCustomer();
customer.name;
customer.email;
The above will need to be in an async
function, like so:
以上将需要在一个async
函数中,如下所示:
async displayCustomerDetails() {
var customer = await getCustomer();
customer.name;
customer.email;
}
回答by Manuel Domínguez
You can use the await operatorlike this:
您可以像这样使用await 运算符:
getCustomer(): Promise<any> {
[...]
}
async functionThatNeedsCustomer() {
const customer = await getCustomer();
const name = customer.email;
const email = customer.email;
}
The await operator awaitsform the Promise to return the result. This can only be done inside an async function (making a function async will make it to return a promise itself).
await 运算符从 Promise等待返回结果。这只能在 async 函数内部完成(使函数 async 将使其本身返回一个 promise)。