如何在node.js中使用.then()?
我是如何在node.js中使用.then()?,node.js,Node.js,我是node.js的初学者。我刚刚读到,我们可以使用.then()函数按特定顺序执行多个函数。我打算这样写代码: function one(){ console.log("one") } function two(){ console.log("two") } function three(){ console.log("three") } one().then(two()).then(three()) 但我得到了一个错误: TypeError: Cannot read prope
node.js
的初学者。我刚刚读到,我们可以使用.then()
函数按特定顺序执行多个函数。我打算这样写代码:
function one(){
console.log("one")
}
function two(){
console.log("two")
}
function three(){
console.log("three")
}
one().then(two()).then(three())
但我得到了一个错误:
TypeError: Cannot read property 'then' of undefined
at Object.<anonymous> (C:\chat\test.js:10:6)
at Module._compile (module.js:570:32)
at Object.Module._extensions..js (module.js:579:10)
at Module.load (module.js:487:32)
at tryModuleLoad (module.js:446:12)
at Function.Module._load (module.js:438:3)
at Module.runMain (module.js:604:10)
at run (bootstrap_node.js:389:7)
at startup (bootstrap_node.js:149:9)
at bootstrap_node.js:502:3
TypeError:无法读取未定义的属性“then”
反对。(C:\chat\test.js:10:6)
在模块处编译(Module.js:570:32)
在Object.Module.\u extensions..js(Module.js:579:10)
在Module.load(Module.js:487:32)
在tryModuleLoad时(module.js:446:12)
在Function.Module.\u加载(Module.js:438:3)
位于Module.runMain(Module.js:604:10)
运行时(bootstrap_node.js:389:7)
启动时(bootstrap_node.js:149:9)
在bootstrap_node.js:502:3
当您需要知道何时完成时,.then()函数用于PROMISE(异步函数也是如此),所以
MYASYNCFUNCTION().then(function(response({
//do what you want when it's finish and everything ok .. with the result
}).catch(function(error){
// it's finshed but with error
})
在你的例子中。。没有.then()函数,因为它们是简单的函数。。但是如果你想拥有它(我不知道为什么。它们里面没有异步的东西……但是你可以)
所以
//通过npm安装承诺安装
var Promise = require('promise');
function one(){
var promise = new Promise(function (resolve, reject) {
resolve('one');
});
});
}
然后
one().then(function(resp){ console.log(resp) })
希望它能帮助你 然后通常用于承诺的上下文中。您可以从这里开始阅读更多关于它的内容:。然后,只有当函数返回承诺时,它才起作用。承诺用于异步任务,因此您可以在执行其他任务之前等待
函数一(){
返回新承诺(解决=>{
设置超时(()=>{
console.log('one'))
解决();
}, 1000);
});
}
函数二(){
返回新承诺(解决=>{
设置超时(()=>{
console.log('two')
解决();
}, 1000);
});
}
职能三(){
返回新承诺(解决=>{
设置超时(()=>{
console.log('three')
解决();
}, 1000);
});
}
一,然后(两),然后(三)代码>。然后,
是一种基于承诺的方法,是一种代码同步机制。您的代码不是异步的,因此不需要使用承诺。你可以打个电话
one();
two();
three();
如果您的代码执行异步操作,那么您可以使用promises和。然后。异步操作包括读/写文件、http请求、计时器等
例如,我们可以使用内置的Promise
创建我们自己的异步操作:
我不建议你通常这样做。我们只是以它为例。在大多数情况下,您可以调用已经返回承诺的函数
还要注意的是,当您使用.then
时,需要传递回调two()
立即调用two
函数,因此它与()=>two()
不同
接下来,您可以经常使用async
/wait
而不是。然后,我认为在大多数情况下,这会使您的代码更容易推理
async function run() {
await one();
await two();
three();
}
run();
这与第二个示例相同,该示例重写为使用wait
而不是。您可以将await
之后的所有内容都视为的内部。然后
链接到await
之后的表达式
最后,您应该通过将.catch
链接到承诺或在异步函数中使用正常的/try
来处理错误。.then()
用于。您还希望传递函数,而不是其返回类型作为.then()
参数。要使示例起作用,请尝试:
function one(){
return Promise.resolve(console.log("one"));
}
function two(){
return Promise.resolve(console.log("two"));
}
function three(){
return Promise.resolve(console.log("three"));
}
one()
.then(two)
.then(three)
.catch(error => {
console.error('uhoh');
});
此外,虽然这可能适用于您的示例。您通常不会使用Promise.resolve()
。您会发现使用构造函数更为典型:
function func(a, b) {
return new Promise((resolve, reject) => {
if (!a || !b) return reject('missing required arguments');
return resolve(a + b);
}
}
在出现错误时调用reject
,在成功时调用resolve
。拒绝被路由到第一个.catch()
。我鼓励您阅读上面链接中的承诺。然后将使用
。这仅适用于承诺和返回承诺的函数。这并不是Node所有方法都可以使用的。在处理同步函数时,您不需要使用then
(您的文章中也有)。如果你写one();二();三个()。如果您在google或stackoverflow上对.then()进行简单搜索,您将拥有大量资源。在发布问题之前进行研究,展示你学到的知识,如果你不能理解,那么发布一个问题。非常感谢先生的详细解释。我真的被这个回答弄糊涂了。@user10664542你能更具体地说明什么让你困惑吗?
function func(a, b) {
return new Promise((resolve, reject) => {
if (!a || !b) return reject('missing required arguments');
return resolve(a + b);
}
}