【问题标题】:error when calling promise function within another function在另一个函数中调用 promise 函数时出错
【发布时间】:2013-08-17 03:50:13
【问题描述】:

当我尝试使用命令.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)

知道为什么吗?

【问题讨论】:

    标签: javascript node.js promise


    【解决方案1】:

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

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

    【讨论】:

    • 这是打破一切不需要蓄意恶意的更有效方法之一(例如delete Array.prototype.forEach或Object.prototype.hasOwnProperty = function() { return 0; };)
    猜你喜欢
    • 2018-12-21
    • 1970-01-01
    • 2019-10-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多