诺言解决有什么问题? [英] What is wrong with promise resolving?

查看:128
本文介绍了诺言解决有什么问题?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有什么想法吗?为什么节点说文件名未定义"?谢谢. 合同,政策和发票功能无需数据即可解决,只需resolve().

Any ideas? Why does node say 'filename is undefined'? Thanks. Contract, policy ans invoice functions resolve with no data, just resolve().

var dc = function(data) {

return new Promise(function(resolve, reject) {

    var filename = 'Test';

    var contract = function() { ... }

    var policy = function() { ... }

    var invoice = function() { ... }

    contract().then(invoice().then(policy().then(function() {
        console.log(filename); // Test
        resolve(filename); // UnhandledPromiseRejectionWarning: Unhandled promise rejection (rejection id: 2): ReferenceError: filename is not defined
    })))
})

}

推荐答案

首先,您不能编写:

contract().then(invoice() ... )

(如果invoice()函数返回另一个函数作为then处理程序,则可以使用)

(that would work if the invoice() function returned another function to act as a then handler)

您必须写:

contract().then(function (value) { invoice() ... })

或者:

contract().then(value => invoice() ... )

或者,如果一个功能应处理另一功能的结果,则可能是这样:

Or maybe this if one function should handle the result of other function:

contract().then(invoice).then(policy).then(function (result) { ... });

必须将作为参数传递给then的是函数,而不是调用函数的结果(在您的示例中这可能是一个承诺).

What you have to pass as an argument to then is a function, not a result of calling a function (which is probably a promise in your example).

我不知道这是否是您的方法中唯一的问题,但这当然是问题之一.当然可以,但是可能不如您期望的那样.

I don't know if that's the only problem with your approach but it is certainly one of the problems. Of course it may work but probably not how you expect.

如果您使用的是ES2017 async/await,则自v7.0起在Node中可用,而不是:

If you use ES2017 async/await that's available in Node since v7.0 then instead of:

contract().then(invoice).then(policy).then((result) => { ... });

您可以使用:

let a = await contract();
let b = await invoice(a);
let c = await policy(b);
// here your `result` is in `c`

甚至这个:

let result = await policy(await invoice(await contract()));

请注意,您只能在用async关键字声明的函数中使用它.这适用于7版以上的Node.对于较旧版本的Node,您可以使用基于生成器的协程,使用语法稍有不同的类似内容,也可以使用Babel来编译代码(如果您愿意的话)做.

Note that you can only use it in functions declared with the async keyword. This works on Node since version 7. For older versions of Node you can use a similar thing with a slightly different syntax using generator-based coroutines, or you can use Babel to transpile your code if that's what you prefer of if that what you already do.

这是一个相当新的功能,但是有关堆栈溢出的问题很多.参见:

This is quite a new feature but there are a lot of questions on Stack Overflow about it. See:

  • try/catch blocks with async/await
  • Do async in a blocking program language way?
  • try/catch blocks with async/await
  • Use await outside async
  • Using acyns/await in Node 6 with Babel
  • When do async methods throw and how do you catch them?
  • using promises in node.js to create and compare two arrays
  • Keeping Promise Chains Readable
  • function will return null from javascript post/get

这篇关于诺言解决有什么问题?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆