调用一个在 node.js 中有承诺的函数
call a function that has a promise in node.js
我有一个有续集查询的函数
getsuperadmin = function(){
var user = User.findOne({
where: { roleid: 1 },
attributes:['userid','username','email']
}).then(user => {
});
return user;
}
我正在尝试在同一个 js 文件和 console.log 的另一个函数中调用此函数 getsuperadmin
,如下所示
getadmindetails(){
console.log(getsuperadmin());
}
我在我的控制台中得到以下输出
Promise [Object] {
_bitField: 0,
_fulfillmentHandler0: undefined,
_rejectionHandler0: undefined,
_promise0: undefined,
_receiver0: undefined }
为什么我得到这个输出而不是实际结果
您可能想要执行以下操作:
async function getadmindetails() {
console.log(await getsuperadmin());
}
等待你的承诺解决
对于上面的数据库调用,这就足够了:
function getsuperadmin() {
var user = User.findOne({
where: { roleid: 1 },
attributes:['userid','username','email']
})
return user;
}
在此处阅读有关承诺的更多信息:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise
您需要使用 async/await 或 .then
.
在 getadmindetails
中获取承诺的结果
async function getadmindetails(){
console.log(await getsuperadmin());
}
或
function getadmindetails(){
getsuperadmin.then(console.log);
}
附带说明一下,在这种情况下,您的 getsuperadmin
只能 return User.findOne
的结果。
getsuperadmin = function(){
return User.findOne({
where: { roleid: 1 },
attributes:['userid','username','email']
});
}
getadmindetails(){
getsuperadmin.then(user => {
console.log(user);
})
}
你的想法是对的,但是你的代码有一些问题。您真的只想将代码的异步部分包装在 Promise
中。然后,您可以调用 .then
已解决的承诺:
const getsuperadmin = function() {
console.log('Fetching user data from server..');
//Wrap your async code in a Promise and return the Promise
return new Promise((resolve, reject) => {
//Here I used setTimeout, but you would replace this bit with your code..
setTimeout(() => resolve(JSON.stringify({
userid: 1,
username: 'John Doe',
email: 'john.doe@-example-.com'
})), 1500);
})
}
//Call then to run some callback code once your async code completes
getsuperadmin().then(user => {
console.log(JSON.parse(user));
})
我有一个有续集查询的函数
getsuperadmin = function(){
var user = User.findOne({
where: { roleid: 1 },
attributes:['userid','username','email']
}).then(user => {
});
return user;
}
我正在尝试在同一个 js 文件和 console.log 的另一个函数中调用此函数 getsuperadmin
,如下所示
getadmindetails(){
console.log(getsuperadmin());
}
我在我的控制台中得到以下输出
Promise [Object] {
_bitField: 0,
_fulfillmentHandler0: undefined,
_rejectionHandler0: undefined,
_promise0: undefined,
_receiver0: undefined }
为什么我得到这个输出而不是实际结果
您可能想要执行以下操作:
async function getadmindetails() {
console.log(await getsuperadmin());
}
等待你的承诺解决
对于上面的数据库调用,这就足够了:
function getsuperadmin() {
var user = User.findOne({
where: { roleid: 1 },
attributes:['userid','username','email']
})
return user;
}
在此处阅读有关承诺的更多信息:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise
您需要使用 async/await 或 .then
.
getadmindetails
中获取承诺的结果
async function getadmindetails(){
console.log(await getsuperadmin());
}
或
function getadmindetails(){
getsuperadmin.then(console.log);
}
附带说明一下,在这种情况下,您的 getsuperadmin
只能 return User.findOne
的结果。
getsuperadmin = function(){
return User.findOne({
where: { roleid: 1 },
attributes:['userid','username','email']
});
}
getadmindetails(){
getsuperadmin.then(user => {
console.log(user);
})
}
你的想法是对的,但是你的代码有一些问题。您真的只想将代码的异步部分包装在 Promise
中。然后,您可以调用 .then
已解决的承诺:
const getsuperadmin = function() {
console.log('Fetching user data from server..');
//Wrap your async code in a Promise and return the Promise
return new Promise((resolve, reject) => {
//Here I used setTimeout, but you would replace this bit with your code..
setTimeout(() => resolve(JSON.stringify({
userid: 1,
username: 'John Doe',
email: 'john.doe@-example-.com'
})), 1500);
})
}
//Call then to run some callback code once your async code completes
getsuperadmin().then(user => {
console.log(JSON.parse(user));
})