Since from co#4.0+ we can use below statement
var fn = co.wrap(fn*)
to convert a generator into a regular function that returns a Promise.
Then I face a problem
a.js
var F = function *(a,b,c){
this.a = yield this.getA(a);
this.b = yield this.getB(b);
this.c = yield this.getC(c);
}
F.prototype.getA = function * (a){
//........
}
F.prototype.getB = function * (b){
//........
}
F.prototype.getC = function * (c){
//........
}
exports.F = F;
how to create a instance in b.js by co .
#Bergi said it`s a bad practice
then I want to ask anthor question
function* F(){
yield this.x = 2;
yield this.y = 3;
}
var obj = {};
var f = F.bind(obj)();
f.next();
f.next();
f.next();
console.log(obj);
// { x: 2, y: 3 }
is it a bad practice ?
Related
I have a cloud function whose object you can see here:
https://us-central1-mathfutures-cf044.cloudfunctions.net/simpleFunction
It should return a new object every time it is called. However, it does not. What is wrong?
Here is my cloud function:
export let simpleFunction = functions.https.onRequest((request, response) => {
buildQuestions();
response.json(questions);
});
function s1plus1() {
let number1 = myRandom(1);
let number2 = myRandom(1);
let problem;
let qtype = "s1+1";
problem = number1 + " + " + number2;
let solution = number1 + number2;
let exc = {
qtype: qtype,
question: problem,
answer: solution,
}
return exc;
}
let numItems = 10;
let questions = [];
let i = 0;
function buildQuestions() {
while (i < numItems) {
questions.push(s1plus1());
i = i + 1;
}
}
The problem was that numItems and i had to be initiated inside buildQuestions, like this:
let questions = [];
function buildQuestions() {
let numItems = 10;
let i = 0;
while (i < numItems) {
questions.push(s1plus1());
i = i + 1;
}
}
I am wondering what the value of answer will be after calling the outerfunc. Especially interested in the reasoning behind it (e.g. Why is the return value of the innerfunc not replacing the value stored in var x in the global scope? ) Thanks!
var x = 10;
function outerfunc () {
x = 20;
function innerfunc () {
var x = x + 20;
return x;
}
innerfunc();
}
outerfunc();
var answer = x;
Re Why is the return value of the innerfunc not replacing the value stored in var x in the global scope?
Because you created a new var called x inside innerfunc. Try removing the var from line var x = x + 20;
When you declare a local var with same name as a global var, then the global var become invisible in that function.
function innerfunc () {
var x = x + 20; // here there is no global x. so it is NaN
return x;
}
See this
var x = 10;
function outerfunc () {
x = 20;
function innerfunc () {
var x = x + 20;
alert("value of x in innerFun " + x);
}
innerfunc();
}
outerfunc();
alert("global value of x is " + x);
I have this collection in MongoDB (I'm omitting _ids for brevity):
test> db.entries.find();
{
"val": 1
}
{
"val": 2
}
{
"val": 3
}
{
"val": 4
}
{
"val": 5
}
I need to perform some processing on each document which I cannot do with db.update(). So, in a nutshell, what I need to do is retrieving one document at a time, process it in Node and save it back to Mongo.
I'm using the Monk library, and Q for promises. Here's what I've done — I didn't include the processing/save bit for brevity:
var q = require('q');
var db = require('monk')('localhost/test');
var entries = db.get('entries');
var i = 1;
var total;
var f = function (entry) {
console.log('i = ' + i);
console.log(entry.val);
i++;
if (i <= total) {
var promise = entries.findOne({ val: i });
loop.then(function (p) {
return f(p);
});
return promise;
}
};
var loop = q.fcall(function () {
return entries.count({});
}).then(function (r) {
total = r;
return entries.findOne({ val: i });
}).then(f);
I would expect this code to print out:
i = 1
1
i = 2
2
i = 3
3
i = 4
4
i = 5
5
but it actually prints out:
i = 1
1
i = 2
2
i = 3
2
i = 4
2
i = 5
2
What am I doing wrong?
In your code, loop is one and only one promise. It is executed only once. It is not a function.
Inside f, loop.then(f) just trigger f with the result of the promise (it has already been executed so it is not executed again).
You actually want to create several promises.
What you are looking for is something that should looks like:
var q = require('q');
var db = require('monk')('localhost/test');
var entries = db.get('entries');
var i = 1;
var total;
var f = function (entry) {
console.log('i = ' + i);
console.log(entry.val);
i++;
if (i <= total) {
// I am not sure why you put entries.findOne here (looks like a mistake,
// its returned value isn't used) but if you really need it to be done
// before loop, then you must pipe it before loop
return entries.findOne({ val: i }).then(loop);
// do not pipe f again here, it is already appended at the end of loop
}
};
function loop(){
return q.fcall(function () {
return entries.count({});
}).then(function (r) {
total = r;
return entries.findOne({ val: i });
}).then(f);
}
loop();
If you are interested, here is a very nice article about promises.
Is it possible to return a Node object from a file to another?
ex:
file_1
myObject = function(x){
this.x = x
this.change = function(){
return myObject("new_value");
}
}
module.exports = myObject;
file_2
F1 = require('file_2');
f1 = new F1("some_value");
new = f1.change();
On my code, new now has "undefined". Is there a way to return the new object?
file_1 should be:
myObject = function(x){
this.x = x
this.change = function(){
return myObject("new_value");
}
}
module.exports = myObject;
I've seen similar questions here, but none of the solutions fixed my problem. I'm trying to extend PixiJS's BitmapText class to create a generic text object:
OS7.Text = function(string, x, y)
{
PIXI.BitmapText.call(this, string, {font:"12px Chicago"});
this.position.x = x;
this.position.y = y;
}
OS7.Text.prototype = Object.create( PIXI.BitmapText.prototype );
OS7.Text.prototype.constructor = OS7.Text;
And then extend that for a simple clock that updates every second:
OS7.Time = function()
{
OS7.Text.call(this, "00:00 AM", 571, 5);
this.position.x = 571 - this.textWidth;
this.updateTime();
this.timeFunc = this.updateTime();
window.setInterval(this.timeFunc, 1000);
};
OS7.Time.prototype = Object.create(OS7.Text.prototype);
OS7.Time.prototype.constructor = OS7.Time;
OS7.Time.prototype.updateTime = function()
{
this.prevText = this.text;
this.date = new Date();
this.hour = this.date.getHours();
this.minute = this.date.getMinutes();
this.zero = "";
this.ampm = "AM";
if ( this.hour > 12 )
{
this.hour -= 12;
this.ampm = "PM";
}
if ( this.hour === 0 )
{
this.hour = 12;
}
if ( this.minute < 10 )
{
this.zero = "0";
}
this.setText( this.hour + ":" + this.zero + this.minute + " " + this.ampm );
if ( this.prevText !== this.text )
{
this.updateText();
}
};
No matter what, I get the error Object [object global] has no method updateText even though that function is in PIXI.BitmapText. Not to mention that the whole timeFunc thing seems superfluous, but before that I got the error Object [object global] has no method updateTime.
Why am I getting this error?
This line looks suspicious:
this.timeFunc = this.updateTime();
timeFunc will be undefined, since you are calling updateTime, and it doesn't return anything. Also a function called from a timer will have window, and not the object bound to this. If you want to retain the object refference, you need to use bind
this.timeFunc = this.updateTime.bind(this);
When your function is called on the time intervals, the value of this won't be the instance of your object. You'll have to wrap it in a function:
var self = this;
window.setInterval(function() { self.updateTime(); }, 1000);