[英]Why this Javascript promise does not work?
I am Learning about promises. 我正在学习诺言。
app.get('/message',function(req, res){
var promise = new Promise(function(resolve, reject){
resolve("hi");
});
promise.then(function(message){
res.json(message);
})
});
This works good. 这很好。 Though This is too simple.
虽然这太简单了。 To write something 'lengthy' I moved the code out of
app.get()
and tried to return the message from the external function... like this: 为了写一些“冗长的东西”,我将代码移出了
app.get()
并试图从外部函数返回消息……
app.get('/message',function(req, res){
var message = message(); // I also tried wrapping this in promise and calling `res.json` in `promise.then()` but no luck
res.json(message);
});
function message(){
var promise = new Promise(function(resolve, reject){
resolve("hi");
});
promise.then(function(message){
return message;
})
}
So why doesn't the return statement in the message()
function return the message ? 那么,为什么
message()
函数中的return语句不返回消息呢? and what's the best practice to move such promising
code out of my route functions? 将此类
promising
代码移出路由功能的最佳实践是什么?
First, you have a local variable named message
which masks the module level variable which has the same name and references a function. 首先,您有一个名为
message
的局部变量,该变量掩盖了具有相同名称并引用函数的模块级变量。 You need to rename one of them. 您需要重命名其中之一。
Then: You don't have a return statement for the message
function, so it returns undefined
. 然后:您没有
message
函数的return语句,因此它返回undefined
。
If you want to get the result of the promise back in the callback function you pass to get
then you need to: 如果你想获得的承诺早在回调函数的结果传递给
get
,那么你需要:
then
on it then
打电话 res.json(...);
res.json(...);
inside the function you pass to then
then
For example: 例如:
app.get('/message',function(req, res){
var my_message = message();
my_message.then(function (data) {
res.json(data);
});
});
function message(){
var promise = new Promise(function(resolve, reject){
resolve("hi");
});
return promise;
}
Your message
function doesn't return anything. 您的
message
功能不返回任何内容。
You could do: 您可以这样做:
app.get('/message',function(req, res){
message().then(function (message) {
res.json(message);
}
});
function message() {
return new Promise(function(resolve, reject){
resolve("hi");
});
}
Also, be careful to not use the same names for multiple variables, since it makes the code error-prone due to less readabililty. 另外,请注意不要对多个变量使用相同的名称,因为由于可读性较低,它会使代码易于出错。
You can return promise from your message function and use async/await on it like: 您可以从消息函数返回promise,并在其上使用async / await,例如:
app.get('/message', async function(req, res){
var msg = await message();
res.json(msg);
});
function message() {
return new Promise(function(resolve, reject){
resolve("hi");
});
}
Like: 喜欢:
function message() { return new Promise(function(resolve, reject) { setTimeout(resolve, 1500, 'hi'); }); } async function go() { console.log('Async call started...'); var msg = await message(); console.log(msg); } go();
The function message does not return the created promise. 该功能消息不返回创建的承诺。 Normally you'd have an error saying: cannot read property .then of undefined
通常,您会遇到一个错误:无法读取属性.then的未定义
function message(){
var promise = new Promise(function(resolve, reject){
resolve("hi");
});
return promise.then(function(message){ // This will return the initial promise. Due to to the .then, that promise is chained into resolving into the message
return message;
})
}
It could be shorted though (in case you do not want the .then in your message function. Just return the promise then: 不过,它可能会被缩短(以防您在消息函数中不希望使用.then。只需返回promise然后:
function message(){
return new Promise(function(resolve, reject){
resolve("hi");
});
} }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.