Reputation: 3004
I have a node.js file that is calling an async function and I keep getting a TypeError where property of "then" cannot be undefined at context.
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/async
exports = typeof window === 'undefined' ? global : window;
exports.asyncAnswers = {
async: function(value) {
},
manipulateRemoteData: function(url) {
}
};
Any help would be greatly appreciated!
Upvotes: 1
Views: 2218
Reputation: 903
You should change your async function in something like this using a Promise object:
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) {
}
};
Upvotes: 0
Reputation: 473
Your async
function in app/async
needs to return a Promise object. Right now, it isn't returning anything.
Upvotes: 1