2016-11-12 5 views
6

私は最新のノードバージョン7.1.0をOSX上で使用していますが、依然としてプロミスを使用することはできません。私はノード7.1.0 new Promise()リゾルバ未定義は関数ではありません

new Promise(); 

エラーindex.js

を得る:

new Promise(); 
      ^

TypeError: Promise resolver undefined is not a function

7.1.0サポートES6と約束ノードではないでしょうか?

+0

コードを表示します。 –

+4

パラメータを 'new Promise()'に渡す必要があります。あなたは何も渡していません。それは関数ではないので、 'undefined'として扱われます。したがって、エラーです。あなたが渡す関数は通常 "リゾルバ"と呼ばれるので、 "Promise executor undefinedは関数ではありません"と言うと、エラーメッセージはもっと役に立ちます。 –

+1

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise – str

答えて

10

約束事のためのAPIでは、約束コンストラクタに関数を渡す必要があります。 MDN引用:あなたが使用例についてはthis answerを見ることができます

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.

ノード7.1は約束をサポートします。

5

Promiseコンストラクタへのコールバックを提供して、操作を解決または拒否するときに何をすべきかを知る必要があります。例えば

:5秒後

var p = new Promise((resolve, reject) => { 
    setTimeout(() => { 
     resolve(); 
    }, 5000); 
}); 

p.then(() => { 
    console.log("Got it"); 
}) 

あなたのコンソールにメッセージGot itが表示されます。

の約束のために非常に良いライブラリがあります:Bluebird

は、同様にMDNマニュアルを参照してください。

私はこの記事が気に入りましたGoogle developersです。

関連する問題