在另一个函数中调用promise函数时出错

时间:2013-08-16 19:46:02

标签: javascript node.js promise

我有以下代码,当我尝试使用命令.load myFile.js

将其加载到交互式节点控制台时出现错误
var Q=require('q');
var promise1 = function(x){
    var def = Q.defer();
    setTimeout(function(){
        def.resolve(x*5);
    },3000);
    return def.promise;
};

var process = function(val){
    promise1(val);
};

这是我的错误:

Failed to load:myFile.js

_stream_writable.js:258
      process.nextTick(function() {
              ^
TypeError: Object function (val){
promise1(val);
} has no method 'nextTick'
    at onwrite (_stream_writable.js:258:15)
    at WritableState.onwrite (_stream_writable.js:97:5)
    at WriteStream.Socket._write (net.js:651:5)
    at doWrite (_stream_writable.js:219:10)
    at writeOrBuffer (_stream_writable.js:209:5)
    at WriteStream.Writable.write (_stream_writable.js:180:11)
    at WriteStream.Socket.write (net.js:613:40)
    at REPLServer.repl.defineCommand.action (repl.js:858:27)
    at REPLServer.parseREPLKeyword (repl.js:670:16)
    at Interface.<anonymous> (repl.js:219:16)

知道为什么吗?

1 个答案:

答案 0 :(得分:4)

process是一个内置的Node.js全局。

通过在REPL中创建自己的process变量(没有自己的范围),你就破坏了一切。