node . js→TypeError: Cannot read property 'then'未定义的
Node.js -> TypeError: Cannot read property 'then' of undefined at Context
我有一个node.js文件,正在调用异步函数,我不断得到TypeError的属性"then"不能在上下文中未定义。
async.js
if ( typeof window === 'undefined' ) {
require('../../app/async');
var expect = require('chai').expect;
}
describe('async behavior', function() {
it('you should understand how to use promises to handle asynchronicity', function(done) {
var flag = false;
var finished = 0;
var total = 2;
function finish(_done) {
if (++finished === total) { _done(); }
}
// This is where the error occurs
asyncAnswers.async(true).then(function(result) {
flag = result;
expect(flag).to.eql(true);
finish(done);
});
asyncAnswers.async('success').then(function(result) {
flag = result;
expect(flag).to.eql('success');
finish(done);
});
expect(flag).to.eql(false);
});
app/异步
exports = typeof window === 'undefined' ? global : window;
exports.asyncAnswers = {
async: function(value) {
},
manipulateRemoteData: function(url) {
}
};
任何帮助都将非常感激!
app/async
中的async
函数需要返回一个Promise对象。现在,它没有返回任何东西
你应该像这样使用Promise对象来改变async函数:
exports = typeof window === 'undefined' ? global : window;
exports.asyncAnswers = {
async: function(value) {
return new Promise(function (resolve, reject){
// DO YOUR STUFF HERE
// use resolve to complete the promise successfully
resolve(returnValueOrObject);
// use reject to complete the promise with an error
reject(errorGenerated);
});
},
manipulateRemoteData: function(url) {
}
};
相关文章:
- 为什么“;未定义的“;在JavaScript中结束循环
- 要求未定义JS回调参数
- 如何检查管道中未定义的项目
- TypeError:无法读取属性'推'未定义的JavaScript
- angularjs无法读取未定义的属性then
- 使用deferred.resolve(Value)返回给my.then()的值未定义
- 未捕获的类型错误:无法调用未定义的方法“then”
- Angularjs/Ionic TypeError:无法读取未定义的属性“then”
- 未捕获的类型错误:调用函数时无法读取未定义的属性“then”
- 获取类型错误:无法在 angularjs 控制器中调用未定义的方法 'then'
- TypeError:无法调用未定义的 Angularjs 的方法 'then'
- 问.js节点承诺.“套接字”上缺少错误处理程序.类型错误:无法调用未定义的方法“then”
- 使用 Promise,收到错误“无法读取未定义的属性'then'”
- 无法读取 nodejs 中未定义的属性“then”
- 在 Angular 应用程序上收到错误:类型错误:无法读取未定义的属性“then”
- node . js→TypeError: Cannot read property 'then'未定义的
- 无法读取property 'then'JavaScript的未定义承诺
- Promise,.then()变量一直表示未定义
- this 变量是 Window 对象,或者在 then 方法的 promise 中未定义
- 我得到错误的angular.js:12520TypeError:不能读取属性'then'未定义的. .