Node 7.1.0 new Promise() 解析器 undefined 不是函数 [英] Node 7.1.0 new Promise() resolver undefined is not a function

查看:148
本文介绍了Node 7.1.0 new Promise() 解析器 undefined 不是函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 OSX 上使用最新的节点版本 7.1.0,但我仍然无法使用 Promises.我明白了

index.js

new Promise();

错误:

<块引用>

new Promise();^

TypeError: Promise resolver undefined 不是函数

node 7.1.0 不支持 ES6 和 Promise 吗?

解决方案

promise 的 API 要求您将函数传递给 promise 构造函数.引用 MDN:

<块引用>

new Promise(/* executor */function(resolve, reject) { ... } );

executor - 一个带有参数 resolve 和 reject 的函数.执行器函数由 Promise 实现立即执行,传递解析和拒绝函数(在 Promise 构造函数甚至返回创建的对象之前调用执行器).resolve 和 reject 函数在调用时分别解析或拒绝承诺.执行器通常会启动一些异步工作,然后在完成后调用 resolve 或 reject 函数来解析 promise,否则在发生错误时拒绝它.

你可以看到这个答案使用示例.

Node 7.1 支持承诺.

I'm using the latest node version 7.1.0 on OSX but I still can't use Promises. I get

index.js

new Promise();

Error:

new Promise();
             ^

TypeError: Promise resolver undefined is not a function

Doesn't node 7.1.0 support ES6 and Promise?

解决方案

The API for promises requires you to pass a function to the promise constructor. Quoting MDN:

new Promise( /* executor */ function(resolve, reject) { ... } );

executor - A function that is passed with the arguments resolve and reject. The executor function is executed immediately by the Promise implementation, passing resolve and reject functions (the executor is called before the Promise constructor even returns the created object). The resolve and reject functions, when called, resolve or reject the promise respectively. The executor normally initiates some asynchronous work and then, once that completes, calls either the resolve or reject function to resolve the promise or else reject it if an error occurred.

You can see this answer for usage examples.

Node 7.1 supports promises.

这篇关于Node 7.1.0 new Promise() 解析器 undefined 不是函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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