I try to run jQuery $.Deferred on node.js.
The working code in Browser is
console = {
log : function(msg) {
$("#console").append(msg);
}
};
var __t = function(time)
{
return $.Deferred(function(dfd)
{
setTimeout(dfd.resolve, time);
})
.promise();
}
__t(2000)
.then(function()
{
console.log("Hello!");
});
So, just trying to migrate to node:
https://www.npmjs.org/package/jquery
npm install jquery
then
app.js
var $ = require('jquery');
var __t = function(time)
{
return $.Deferred(function(dfd)
{
setTimeout(dfd.resolve, time);
})
.promise();
};
__t(2000)
.then(function()
{
console.log("Hello");
});
The error goes:
/....../app.js:5
return jquery.Deferred(function(dfd)
^
TypeError: Object function ( w ) {
if ( !w.document ) {
throw new Error( "jQuery requires a window with a document" );
}
return factory( w );
} has no method 'Deferred'
at __t (/....../app.js:5:18)
at Object.<anonymous> (/....../app.js:12:2)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Function.Module.runMain (module.js:497:10)
at startup (node.js:119:16)
at node.js:906:3
What do I miss?
Thanks.
EDIT
Thanks to the comment, I try to use
https://github.com/kriskowal/q
instead.
However, some error occurred.
/Volumes/GD15/gd1/_book_project/app.js:5
return $.Deferred(function(dfd)
^
TypeError: Object function Q(value) {
// If the object is already a Promise, return it directly. This enables
// the resolve function to both be used to created references from objects,
// but to tolerably coerce non-promises to promises.
if (isPromise(value)) {
return value;
}
// assimilate thenables
if (isPromiseAlike(value)) {
return coerce(value);
} else {
return fulfill(value);
}
} has no method 'Deferred'
at __t (/Volumes/GD15/gd1/_book_project/app.js:5:13)
at Object.<anonymous> (/Volumes/GD15/gd1/_book_project/app.js:12:2)
at Module._compile (module.js:456:26)
at Object.Module._extensions..js (module.js:474:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:312:12)
at Function.Module.runMain (module.js:497:10)
at startup (node.js:119:16)
at node.js:906:3